ImportedPage Class | PdfDocument Class | Performance Considerations | Programming with Merger for .NET

Language

Visual Basic

C#

Show All

The ImportedPage class can be used to import a page from an existing document as a template and add page elements to it. The dimensions of the imported page are preserved.

The following code will import a page and add page elements to it.

[Visual Basic]
Dim MyDocument As Document = New Document()
Dim MyPage As ImportedPage = New ImportedPage( "C:\DocumentA.pdf", 1 )
MyPage.Elements.Add( New Label( "Label Text", 100, 100, 100, 12 ) )
MyPage.Elements.Add( New Label( "Label Text2", 100, 120, 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.Elements.Add( new Label( "Label Text", 100, 100, 100, 12 ) );
page.Elements.Add( new Label( "Label Text2", 100, 120, 100, 12 ) );
document.Pages.Add( page );
document.Draw( @"C:\MyDocument.pdf" );

Importing a Page From a Stream

The following code shows how to import a page from a stream object.

[Visual Basic]
Dim MyDocument As Document = New Document()
Dim MyPdfDocument As PdfDocument = New PdfDocument( MyStream )
Dim MyPage As ImportedPage = New ImportedPage( MyPdfDocument.GetPage(1) )
MyPage.Elements.Add( New Label( "Label Text", 100, 100, 100, 12 ) )
MyPage.Elements.Add( New Label( "Label Text2", 100, 120, 100, 12 ) )
MyDocument.Pages.Add( MyPage )
MyDocument.Draw( "C:\MyDocument.pdf" )
[C#]
Document document = new Document();
PdfDocument pdfDocument = new PdfDocument( stream );
ImportedPage page = new ImportedPage( pdfDocument.GetPage( 1 ) );
page.Elements.Add( new Label( "Label Text", 100, 100, 100, 12 ) );
page.Elements.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 pages that are not going to be reused multiple times. For details on how to most efficiently handle pages that will be used multiple times, see the Performance Considerations topic.

See Also 

ImportedPage Class | PdfDocument Class | Performance Considerations | Programming with Merger for .NET