Adding New Content

There are several ways to add new content to a document including adding new pages to existing documents or to any imported page or page in an imported PDF document.

Adding New Pages to An Existing PDF Document

You can add new pages to the beginning, end, or middle of an imported PDF document. You can then place page elements on these pages. You can also use the ImportedPage class to add existing pages from a separate PDF document. The following code illustrates adding a cover page to a PDF document.

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.Append(pdfFilePath);
document.Draw(pdfFilePath);        
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.Append( pdfFilePath )
MyDocument.Draw(pdfFilePath)  

Adding New Content to An Existing PDF Page

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)  

Add page elements beneath the content of the imported page by adding page elements to the imported page's BackgroundElements collection. The following code imports a page and adds a background to the page.

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)  

NOTE: 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 the Performance Considerations.

In this topic