Does the PDF Library Support Multithreading and Is It Thread-Safe?
24 Jul 202610 minutes to read
Yes, the Essential® PDF library supports creating or modifying PDF documents concurrently in a multi-threaded environment. Thread safety is enabled by setting the EnableThreadSafe property of the PdfDocument class to true.
NOTE
When
EnableThreadSafeis set to true, the PDF library synchronizes access to shared resources internally, allowing multiple threads to safely create and modify PDF documents simultaneously.
Creating a PDF Document in a Multi-Threading Environment
The following code samples demonstrate how to create a PDF document in a multi-threaded environment using Parallel.ForEach.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
IEnumerable<int> works = Enumerable.Range(0, 100);
Parallel.ForEach(works, index => GeneratePDF(index));
private void GeneratePDF(int index)
{
//Enable thread safety in the PDF document.
PdfDocument.EnableThreadSafe = true;
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
string name = Guid.NewGuid().ToString();
//Save the document.
document.Save(name + ".pdf");
//Close the document.
document.Close(true);
}using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
IEnumerable<int> works = Enumerable.Range(0, 100);
Parallel.ForEach(works, index => GeneratePDF(index));
private void GeneratePDF(int index)
{
//Enable thread safety in the PDF document.
PdfDocument.EnableThreadSafe = true;
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
string name = Guid.NewGuid().ToString();
//Save the document.
document.Save(name + ".pdf");
//Close the document.
document.Close(true);
}Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Dim works As IEnumerable(Of Integer) = Enumerable.Range(0, 100)
Parallel.ForEach(works, Sub(index) GeneratePDF(index))
Private Sub GeneratePDF(ByVal index As Integer)
'Enable thread safety in the PDF document.
PdfDocument.EnableThreadSafe = True
'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the standard font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20)
'Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
Dim name As String = Guid.NewGuid().ToString()
'Save the document.
document.Save(name + ".pdf")
'Close the document.
document.Close(True)
End SubYou can download a complete working sample from GitHub.
Modifying an Existing PDF Document in a Multi-Threading Environment
To modify an existing PDF document in a multi-threaded environment, set the EnableThreadSafe property to true and load the PDF document using the PdfLoadedDocument class. The following code samples illustrate this scenario.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
IEnumerable<int> works = Enumerable.Range(0, 100);
Parallel.ForEach(works, index => ModifyPDF(index));
private void ModifyPDF(int index)
{
//Enable thread safety in the PDF document.
PdfDocument.EnableThreadSafe = true;
//Load a PDF document.
PdfLoadedDocument doc = new PdfLoadedDocument("input.pdf");
//Get the first page from the document.
PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
string name = Guid.NewGuid().ToString();
//Save the document.
doc.Save(name + ".pdf");
//Close the document.
doc.Close(true);
}using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
IEnumerable<int> works = Enumerable.Range(0, 100);
Parallel.ForEach(works, index => ModifyPDF(index));
private void ModifyPDF(int index)
{
//Enable thread safety in the PDF document.
PdfDocument.EnableThreadSafe = true;
//Load a PDF document.
PdfLoadedDocument doc = new PdfLoadedDocument("input.pdf");
//Get the first page from the document.
PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
string name = Guid.NewGuid().ToString();
//Save the document.
doc.Save(name + ".pdf");
//Close the document.
doc.Close(true);
}Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Parsing
Dim works As IEnumerable(Of Integer) = Enumerable.Range(0, 100)
Parallel.ForEach(works, Sub(index) ModifyPDF(index))
Private Sub ModifyPDF(ByVal index As Integer)
'Enable thread safety in the PDF document.
PdfDocument.EnableThreadSafe = True
'Load a PDF document.
Dim doc As PdfLoadedDocument = New PdfLoadedDocument("input.pdf")
'Get the first page from the document.
Dim page As PdfLoadedPage = doc.Pages(0)
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the standard font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20)
'Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
Dim name As String = Guid.NewGuid().ToString()
'Save the document.
doc.Save(name + ".pdf")
'Close the document.
doc.Close(True)
End SubYou can download a complete working sample from GitHub.