Adding New Content

There are several ways to add new content to a document, including adding new pages, adding existing pages, and appending 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. You can create new pages or add existing pages to a MergeDocument.

You can add new page elements both when creating a new page and when importing pages. Examples of both are provided in the examples below.

To extract text or other page elements from an existing page, refer to the PdfDocument and PdfPage classes (see Extracting Text).

Adding New Pages

Add new pages to an imported PDF document's beginning, middle, or end using the MergeDocument class's Add or Insert methods.

To add a new page, create a new Page instance, add the desired page elements, and then add the page to the MergeDocument instance.

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 document As New MergeDocument()
Dim page As New Page(PageSize.Letter, PageOrientation.Portrait)
page.Elements.Add(New Label("Cover Page", 0, 0, 512, 12))
document.Pages.Add(page)          document.Append("DocumentA.pdf")
document.Draw(outputPath)

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

MergeDocument document = new MergeDocument("DocumentA.pdf");
Page page = new Page(PageSize.Letter, PageOrientation.Portrait);
page.Elements.Add(new Label("Cover Page", 0, 0, 512, 12));
document.Pages.Insert(1, page);
document.Draw(outputPath);
Dim document As New MergeDocument("DocumentA.pdf")
Dim page As New Page(PageSize.Letter, PageOrientation.Portrait)
page.Elements.Add(New Label("Cover Page", 0, 0, 512, 12))
document.Pages.Insert(1, page)
document.Draw(outputPath)

Adding Content To Existing Page

Add new content to any page from a PdfDocument imported to a MergeDocument instance by obtaining the page from the MergeDocument class and then adding page elements to the page.

Recall the PdfDocument and PdfPage classes are read-only and not modifiable.

The following example illustrates adding new content to an existing page in a MergeDocument instance.

MergeDocument document = new MergeDocument("DocumentC.pdf");
Page page = document.Pages[1];
page.Elements.Add(new Label("New Content", 0, 0, 512, 12));
document.Draw(outputPath);      
Dim document As New MergeDocument("DocumentC.pdf")
Dim page As Page = document.Pages(1)
page.Elements.Add(New Label("New Content", 0, 0, 512, 12))
document.Draw(outputPath)

Adding Form Field

You can add new form fields to an existing form. The following example illustrates removing an existing form field and adding a new form field.

MergeDocument document = new MergeDocument("AllFormFields.pdf");
document.Form.Fields[0].Output = FormFieldOutput.Remove;
Page pg = document.Pages[0];
CheckBox checkBox = new CheckBox("added_chk", 70, 90, 20, 20);
checkBox.DefaultChecked = true;
pg.Elements.Add(checkBox);
document.Draw(outputFile);
Dim document As New MergeDocument("AllFormFields.pdf")
document.Form.Fields(0).Output = FormFieldOutput.Remove
Dim pg As Page = document.Pages(0)
Dim checkBox As New CheckBox("added_chk", 70, 90, 20, 20)
checkBox.DefaultChecked = True
pg.Elements.Add(checkBox)
document.Draw(outputPath)

Adding Existing Page

Add a page to a MergeDocument instance from an existing page by using the ImportedPage class.

MergeDocument document = new MergeDocument("DocumentC.pdf");
ImportedPage page = new ImportedPage("DocumentA.pdf", 1);
document.Pages.Insert(0, page);
document.Draw(outputPath); 
Dim document As New MergeDocument("DocumentC.pdf")
Dim page As New ImportedPage("DocumentA.pdf", 1)
document.Pages.Insert(0, page)
document.Draw(outputPath)

You can also import pages when creating a new PDF document. Refer to the Adding Existing Pages documentation topic.

You can also add content to an ImportedPage. To add content to the page's background, add the element to the BackgroundElements collection. To add to the foreground, add the element to the page's Elements collection.

The following example illustrates importing a page and then adding an image to the page's elements and a rectangle to the page's background elements.

MergeDocument document = new MergeDocument();
ImportedPage page = new ImportedPage("DocumentC.pdf", 1);
page.Elements.Add(new Image("bluerectangle.png"), 300, 175, 1);
Label lbl = new Label("Label Text", 50, 75, 200, 12);
lbl.FontSize = 72;
lbl.TextColor = RgbColor.White;
lbl.TextOutlineColor = RgbColor.Red;
lbl.TextOutlineWidth = 3;
page.Elements.Add(lbl);
Rectangle rec = new Rectangle(10, 50, 250, 500, RgbColor.Black, RgbColor.DarkGreen);
page.BackgroundElements.Add(rec);
document.Pages.Add(page);
document.Draw(outputPath);
Dim document As New MergeDocument()
Dim page As New ImportedPage("DocumentC.pdf", 1)
page.Elements.Add(New Image("bluerectangle.png"), 300, 175, 1))
Dim lbl As New Label("Label Text", 50, 75, 200, 12) With {
                .FontSize = 72,
                .TextColor = RgbColor.White,
                .TextOutlineColor = RgbColor.Red,
                .TextOutlineWidth = 3
}
page.Elements.Add(lbl)
Dim rec As New Rectangle(10, 50, 250, 500, RgbColor.Black, RgbColor.DarkGreen)
page.BackgroundElements.Add(rec)
document.Pages.Add(page)
document.Draw(outputPath)

Adding Content Figure 1. Importing a page and adding content.

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.

Appending PDF Documents

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 code loads a PDF document, appends a range of pages from one document to it, and then appends another document to the PDF.

MergeDocument document = new MergeDocument(pdfFilePath);
document.Append(pdfFilePath, 1, 2 );
document.Append(pdfFilePath);
document.Draw(pdfFilePath);        
Dim MyDocument As MergeDocument = New MergeDocument( pdfFilePath )
MyDocument.Append(pdfFilePath, 1, 2 )
MyDocument.Append(pdfFilePath)
MyDocument.Draw(pdfFilePath)   

In this topic