Performance Considerations

When merging, several performance considerations will significantly reduce resource usage.

PdfDocument

The PdfDocument class holds the pages and resources of a PDF document and is used in the MergeDocument, ImportedPage, and ImportedPageData constructors. The PdfDocument class is also used by a MergeDocument instance's Append method. Reusing a PdfDocument instance optimizes performance by minimizing the times that PDF data needs to be parsed and allowing for efficiently reusing shared PDF resources.

Use Statically

The PdfDocument class is thread-safe and can be instantiated as a static instance (Shared in Visual Basic .NET) of a class member variable. The PdfDocument instance can then be reused as needed. Reusing a PdfDocument instance is recommended for documents or pages from documents, such as templates or cover pages, that will be reused many times in your application. Consider the following example: a PdfDocument is instantiated statically and then reused when getting its pages.

private static PdfDocument pdfDocument = new PdfDocument("myInputPdf.pdf");

static void Merge (string outputPdf)
{
   MergeDocument document = new MergeDocument();

   for (int i = 0; i < pdfDocument.Pages.Count; i++)
   {
     ImportedPage page = new ImportedPage(pdfDocument.GetPage(i));
     document.Pages.Add(page);
   }

   document.Draw(outputPdf);
}

Avoid Using File Paths

Use a PdfDocument class anywhere a file path to an existing PDF document can be specified. The MergeDocument, ImportedPage, and ImportedPageData constructors can all accept a file path rather than a PdfDocument instance. The MergeDocument Append method also allows using a file path rather than a PdfDocument instance. However, consider that when specifying a file path, the PdfDocument instance must be first loaded from the disk and instantiated. File IO and object instantiation are both costly activities. To avoid needless resource use, only specify a PDF by file path if not reused; otherwise, instantiate it first and then use the PdfDocument instance.

Example

The following example shows how to efficiently reuse a template page and PDF document in your class.

private static PdfDocument template = new PdfDocument(pdfFilePath);
private static PdfDocument legalPages = new PdfDocument(pdfFilePath);
// Create a MergeDocument object
MergeDocument document = new MergeDocument();
// Create a page out of the first page in the template PDF
ImportedPage page = new ImportedPage( template.GetPage( 1 ) );
// Add the full name to the template
page.Elements.Add( new Label( "Full Name", 0, 0, 200, 12 ) );
// Add the page to the document
document.Pages.Add( page );
// Append all the legal pages to the end of the document
document.Append( legalPages );
// Save the PDF
document.Draw(pdfFilePath);       
Private Shared MyTemplate As PdfDocument = New PdfDocument(pdfFilePath)
Private Shared MyLegalPages As PdfDocument = New PdfDocument(pdfFilePath)
' Create a MergeDocument object
Dim MyDocument As MergeDocument = New MergeDocument()
' Create a page out of the first page in the template PDF
Dim MyPage As ImportedPage = New ImportedPage( MyTemplate.GetPage( 1 ) )
' Add the full name to the template
MyPage.Elements.Add( New Label( "Full Name", 0, 0, 200, 12 ) )
' Add the page to the document
MyDocument.Pages.Add( MyPage )
' Append all the legal pages to the end of the document
MyDocument.Append( MyLegalPages )
' Save the PDF
MyDocument.Draw(pdfFilePath)

In this topic