New content can be added to a document in several different ways. New pages can be added to existing documents and new Page Elements can be added to any imported page or page in an imported PDF document.
Adding New Pages to An Existing PDF Document
New pages can be added to the beginning or end or inserted in the middle of an imported PDF document. Page Elements from DynamicPDF™ Generator for .NET can then be placed on these pages. It is also possible to add existing pages from a separate PDF document using the ImportedPage class.
The following code shows how to add a cover page to a PDF document.
[Visual Basic]
Dim MyDocument As MergeDocument = New MergeDocument( "C:\DocumentA.pdf" )
Dim MyPage As Page = New Page( PageSize.Letter, PageOrientation.Portrait )
MyPage.Elements.Add( New Label( "Cover Page", 0, 0, 512, 12 ) )
MyDocument.Pages.Insert( 0, MyPage )
MyDocument.Draw( "C:\MyDocument.pdf" )
[C#]
MergeDocument document = new MergeDocument( @"C:\DocumentA.pdf" );
Page page = new Page( PageSize.Letter, PageOrientation.Portrait );
page.Elements.Add( new Label( "Cover Page", 0, 0, 512, 12 ) );
document.Pages.Insert( 0, page );
document.Draw( @"C:\MyDocument.pdf" );
Adding New Content to An Existing PDF Page
New content can be added to any imported page from an existing PDF document. This is accomplished by adding Page Elements from DynamicPDF™ Generator for .NET to the imported pages Elements collection.
The following code shows how to adds new content to an existing page in a PDF document.
[Visual Basic]
Dim MyDocument As MergeDocument = New MergeDocument( "C:\DocumentA.pdf" )
Dim MyPage As Page = MyDocument.Pages(0)
MyPage.Elements.Add( New Label( "New Content", 0, 0, 512, 12 ) )
MyDocument.Pages.Insert( 0, MyPage )
MyDocument.Draw( "C:\MyDocument.pdf" )
[C#]
MergeDocument document = new MergeDocument( @"C:\DocumentA.pdf" );
Page page = document.Pages[0];
page.Elements.Add( new Label( "New Content", 0, 0, 512, 12 ) );
document.Pages.Insert( 0, page );
document.Draw( @"C:\MyDocument.pdf" );
Page elements can be added beneath the content of the imported page by adding page elements to the imported pages BackgroundElements collection.
The following code will import a page and add a background to it.
[Visual Basic]
Dim MyDocument AS Document = New Document()
Dim MyPage As ImportedPage = New ImportedPage( "C:\DocumentA.pdf", 1 )
MyPage.BackgroundElements.Add( New Image( "C:\BackgroundImage.png", 200, 200, 1 ) )
MyPage.BackgroundElements.Add( New Label( "Label Text", 100, 100, 100, 12 ) )
MyDocument.Pages.Add( MyPage )
MyDocument.Draw( "C:\MyDocument.pdf" )
[C#]
Document document = new Document();
ImportedPage page = new ImportedPage( @"C:\DocumentA.pdf", 1 );
page.BackgroundElements.Add( new Image( @"C:\BackgroundImage.png", 200, 200, 1 ) );
page.BackgroundElements.Add( new Label( "Label Text2", 100, 120, 100, 12 ) );
document.Pages.Add( page );
document.Draw( @"C:\MyDocument.pdf" );
NOTE: The examples in this topic show how to import documents and pages that are not going to be reused multiple times. For details on how to most efficiently handle documents and pages that will be used multiple times, see the Performance Considerations topic.
| See Also |
ImportedPage Class | BackgroundElements Property | Page Class | Page Elements | Programming with Merger for .NET

