Adding New Content

There are several ways to add new content to a document, including adding new pages or appending pre-existing PDF documents. Although the MergeDocument class is for merging pre-existing PDFs to create a new PDF, you are not restricted from adding new content, as the MergeDocument class is a child of the Document class. The following examples illustrate adding created content to a MergeDocument instance and using Append to add pre-existing PDFs to a MergeDocument instance.

The final example illustrates how to add a pre-existing PDF to a Document instance rather than a MergeDocument instance. In situations where you are already using the Document class in your code and then need to append a PDF, you can use the ImportedPage class to add a pre-existing PDF. This example illustrates adding pre-existing PDF to a Document instance.

Adding New Pages

You can add new pages to an imported PDF document's beginning, end, or middle.

Pages is a property inherited from the MergeDocument class's parent, Document. The Pages property is an instance of a PageList.

To add a new page, create a new Page instance and call the Pages property's Add method.

MergeDocument document = new MergeDocument();
Page page = new Page( PageSize.Letter, PageOrientation.Portrait );
page.Elements.Add( new Label( "Cover Page", 0, 0, 512, 12 ) );
document.Pages.Add( page );
document.Draw(pdfOutputFilePath);        
Dim MyDocument As MergeDocument = New MergeDocument()
Dim MyPage As Page = New Page( PageSize.Letter, PageOrientation.Portrait )
MyPage.Elements.Add( New Label( "Cover Page", 0, 0, 512, 12 ) )
MyDocument.Pages.Add( MyPage )
MyDocument.Draw(pdfOutputFilePath)  

Appending Pre-Existing PDFs

Use the Append method to append a pre-existing PDF. The Append method is overloaded to allow using a file path, a PdfDocument, limiting the number of pages appended, and several other options (see the API for details). The following example illustrates appending a pre-existing PDF using a file path.

MergeDocument document = new MergeDocument();
document.Append(pdfPreExistingFilePath);
document.Draw(pdOutputfFilePath);        
Dim MyDocument As MergeDocument = New MergeDocument()
MyDocument.Append( pdfPreExistingFilePath )
MyDocument.Draw(pdfOutputFilePath)  

Adding New Content

Add new content to any imported page using an existing PDF document by adding Page Elements from DynamicPDF Core Suite for .NET to the imported page's Elements collection. The following code illustrates adding new content to an existing page in a PDF document.

MergeDocument document = new MergeDocument( pdfFilePath );
Page page = document.Pages[0];
page.Elements.Add( new Label( "New Content", 0, 0, 512, 12 ) );
document.Draw(pdfFilePath);        
Dim MyDocument As MergeDocument = New MergeDocument( pdfFilePath )
Dim MyPage As Page = MyDocument.Pages(0)
MyPage.Elements.Add( New Label( "New Content", 0, 0, 512, 12 ) )
MyDocument.Draw(pdfFilePath)  

ImportedPage to Append Pre-existing PDFs

The final example illustrates adding content to a Document instance using an ImportedPage. An ImportedPage class constructor is overloaded to take a PdfPage instance or a string to a PDF file path. A Document instance can then access its Pages property and Add method to add the page. In the final example, an ImportedPage instance adds an Image and a Label to a page using the page's BackgroundElements collection and then adds the page to the document.

Document document = new Document();
ImportedPage page = new ImportedPage( pdfFilePath, 1 );
page.BackgroundElements.Add( new Image( pngFilePath, 200, 200, 1 ) );
page.BackgroundElements.Add( new Label( "Label Text2", 100, 120, 100, 12 ) );
document.Pages.Add( page );
document.Draw(pdfFilePath);        
Dim MyDocument AS Document = New Document()
Dim MyPage As ImportedPage = New ImportedPage( pdfFilePath, 1 )
MyPage.BackgroundElements.Add( New Image( pngFilePath, 200, 200, 1 ) )
MyPage.BackgroundElements.Add( New Label( "Label Text", 100, 100, 100, 12 ) )
MyDocument.Pages.Add( MyPage )
MyDocument.Draw(pdfFilePath)  

The examples in this topic illustrate importing documents and pages that are not reused multiple times. For details on efficiently handling documents and pages used multiple times, refer to Performance Considerations.

In this topic