DynamicPDF Core Suite for .NET is designed to allow highly efficient reuse of imported PDF document data. The PdfDocument class is included for this purpose. This class holds the pages and resources of a PDF document and can be used in the constructors of MergeDocument, ImportedPage and ImportedPageData as well as the Append method of MergeDocument. Using a PdfDocument object optimizes performance by minimizing the number of times that PDF data needs to be parsed and allowing for the efficient reuse of shared PDF resources.
PdfDocument objects are thread safe and can be added set to a static (Shared in Visual Basic .NET) member variable of your class. This static member variable can then be used wherever the document or pages from the document are needed. This is highly recommended for documents, or pages from documents, such as templates or cover pages, which will be reused many times in your application.
IMPORTANT: A PdfDocument class can be used anywhere a file path to an existing PDF document can be specified such as the constructors of MergeDocument, ImportedPage and ImportedPageData as well as the Append method of MergeDocument. The overloads of those constructors and methods allow PDFs to be specified by file path for convenience. PDFs should only be specified by file path there if they are not going to be reused.
The following example shows how to most 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)