Convert PDF to PNG in Multi-thread mode using C# and .NET
Document processing is an important task for automation systems, web applications, and server-side solutions. One common task is converting PDF files to images,
particularly PNG. In modern programming, it's important not only to achieve high-quality results but also to do so efficiently, taking advantage
of multithreading to improve performance.
In this article, we'll look at how to convert PDF to PNG in multithreaded mode in C# and .NET using the
SautinSoft's SDK from PDF Focus .NET.
This component allows for quick and easy conversion while maintaining high image quality and enabling the processing of multiple documents in parallel.
PNG images are widely used to display PDF pages in web interfaces, mobile applications, and automation systems. This format offers high image quality, transparency support, stable display, and advanced compression capabilities.
Advantages of multithreading:
- Process multiple documents simultaneously.
- Reduce overall task execution time.
- Improve infrastructure efficiency when working with large amounts of data.
When using multithreading, it's important to ensure proper thread organization to avoid issues with locks or resource leaks. In the case of the PDF Focus library, this is achieved by calling transformations in separate threads, allowing for rapid processing of large volumes of work.
What is the use of this example in real-world situations?
- Automating thumbnail generation – static preview PDFs.
- Creating image archives for search and indexing.
- Ensuring fast document viewing in online services.
- Processing large data sets in content management systems.
Using multithreading for PDF file processing is a common practice in large-scale systems where speed and performance are critical.
Such solutions are being implemented in:
- Electronic document management systems.
- Cloud storage.
- Web applications for document processing.
- Automatic printing and archiving systems.
P.S.: This provides significant performance benefits, especially when processing hundreds or thousands of documents.
Complete code
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using SautinSoft;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Before starting, we recommend to get a free key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// SautinSoft.PdfFocus.SetLicense("...");
ConvertPdfToPngInThread();
}
public class TArgument
{
public string PdfFile { get; set; }
public int PageNumber { get; set; }
}
public static void ConvertPdfToPngInThread()
{
string pdfs = Path.GetFullPath(@"..\..\..");
string[] files = Directory.GetFiles(pdfs, "*.pdf");
List<Thread> threads = new List<Thread>();
for (int i = 0; i < files.Length; i++)
{
TArgument targ = new TArgument()
{
PdfFile = files[i],
PageNumber = 1
};
var t = new Thread((a) => ConvertToPng(a));
t.Start(targ);
threads.Add(t);
}
foreach (var thread in threads)
thread.Join();
Console.WriteLine("Done!");
}
public static void ConvertToPng(object targ)
{
TArgument targum = (TArgument)targ;
string pdfFile = targum.PdfFile;
int page = targum.PageNumber;
string pngFile = Path.GetFileNameWithoutExtension(pdfFile) + ".png";
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
f.ImageOptions.ImageFormat = PdfFocus.CImageOptions.ImageFormats.Png;
f.ImageOptions.Dpi = 300;
f.OpenPdf(pdfFile);
bool done = false;
if (f.PageCount > 0)
{
if (page >= f.PageCount)
page = 1;
f.ImageOptions.PageIndex = page - 1;
if (f.ToImage(pngFile) == 0)
done = true;
f.ClosePdf();
}
if (done)
{
Console.WriteLine("{0}\t - Done!", Path.GetFileName(pdfFile));
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(pngFile) { UseShellExecute = true });
}
else
Console.WriteLine("{0}\t - Error!", Path.GetFileName(pdfFile));
}
}
}
Option Infer On
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Threading
Imports SautinSoft
Namespace Sample
Friend Class Sample
Shared Sub Main(ByVal args() As String)
' Before starting, we recommend to get a free key:
' https://sautinsoft.com/start-for-free/
' Apply the key here
' SautinSoft.PdfFocus.SetLicense("...");
ConvertPdfToPngInThread()
End Sub
Public Class TArgument
Public Property PdfFile() As String
Public Property PageNumber() As Integer
End Class
Public Shared Sub ConvertPdfToPngInThread()
Dim pdfs As String = Path.GetFullPath("..\..\..")
Dim files() As String = Directory.GetFiles(pdfs, "*.pdf")
Dim threads As New List(Of Thread)()
For i As Integer = 0 To files.Length - 1
Dim targ As New TArgument() With {
.PdfFile = files(i),
.PageNumber = 1
}
Dim t = New Thread(Sub(a) ConvertToPng(a))
t.Start(targ)
threads.Add(t)
Next i
For Each thread In threads
thread.Join()
Next thread
Console.WriteLine("Done!")
End Sub
Public Shared Sub ConvertToPng(ByVal targ As Object)
Dim targum As TArgument = DirectCast(targ, TArgument)
Dim pdfFile As String = targum.PdfFile
Dim page As Integer = targum.PageNumber
Dim pngFile As String = Path.GetFileNameWithoutExtension(pdfFile) & ".png"
Dim f As New SautinSoft.PdfFocus()
f.ImageOptions.ImageFormat = PdfFocus.CImageOptions.ImageFormats.Png
f.ImageOptions.Dpi = 300
f.OpenPdf(pdfFile)
Dim done As Boolean = False
If f.PageCount > 0 Then
If page >= f.PageCount Then
page = 1
End If
f.ImageOptions.PageIndex = page - 1
If f.ToImage(pngFile) = 0 Then
done = True
End If
f.ClosePdf()
End If
If done Then
Console.WriteLine("{0}" & vbTab & " - Done!", Path.GetFileName(pdfFile))
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(pngFile) With {.UseShellExecute = True})
Else
Console.WriteLine("{0}" & vbTab & " - Error!", Path.GetFileName(pdfFile))
End If
End Sub
End Class
End Namespace
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: