Adding and removing a new page

Creating a page 


Firstly, you need to load an existing document by using the PdfImportedDocument class and then add a new page to the loaded document by calling doc.Pages.Add method. The following example illustrates how to do that.

C#

PdfImportedDocument lDoc = new PdfImportedDocument("myfile.pdf");
PdfPage page = lDoc.Pages.Add() as PdfPage;
// Use the predefined fonts to draw the text.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10.0f, PdfFontStyle.Regular);
PdfGraphics g = page.Graphics;
g.DrawString("My Text", font, PdfBrushes.Black, PointF.Empty);
lDoc.Save("AddNewPages.pdf");
lDoc.Close();

VB.NET

Dim lDoc As New PdfImportedDocument("myfile.pdf")
Dim page As PdfPage = TryCast(lDoc.Pages.Add(), PdfPage)
' Use the predefined fonts to draw the text.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 10.0F, PdfFontStyle.Regular)
Dim g As PdfGraphics = page.Graphics
g.DrawString("My Text", font, PdfBrushes.Black, PointF.Empty)
lDoc.Save("AddNewPages.pdf")
lDoc.Close()

Removing a page 


You can also remove pages from the existing PDF document by using the Remove or RemoveAt methods of the PdfImportedPageCollection class.


The following code example illustrates this.

C#:

PdfImportedDocument lDoc = new PdfImportedDocument("myfile.pdf");
// Remove the first page.
lDoc.Pages.RemoveAt(0);
lDoc.Pages.Remove(lDoc.Pages[0]);
lDoc.Save("AddNewPages.pdf");
lDoc.Close();

VB.NET

Dim lDoc As New PdfImportedDocument("myfile.pdf")
' Remove the first page.
lDoc.Pages.RemoveAt(0)
lDoc.Pages.Remove(lDoc.Pages(0))
lDoc.Save("AddNewPages.pdf")
lDoc.Close()