Add new pages

Docotic.Pdf Library Help > Samples > Pages and Navigation > Add new pages

This samples shows how to add or insert pages to your PDF documents.

You can access the collection of PDF document pages using PdfDocument.Pages property. Use PdfDocument.AddPage() method to add a new page to the end of the document. Use PdfDocument.InsertPage(int position) method to insert a new page to an arbitrary position in the document. Valid range for position argument is (0..Pages.Count) inclusive.

Call to the InsertPage method with position argument equal to PdfDocument.Pages.Count is the same as call to the PdfDocument.AddPage() method. Both of AddPage and InsertPage methods return the newly created page.

CopyC#
using System.Diagnostics;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class AddPages
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            PdfDocument pdf = new PdfDocument();

            // There is already one page in PDF document after creation
            PdfPage firstPage = pdf.Pages[0];
            firstPage.Canvas.DrawString("This is the auto-created first page");

            // AddPage method adds a new page to the end of the PDF document
            PdfPage secondPage = pdf.AddPage();
            secondPage.Canvas.DrawString("Second page");

            // You can insert a new page in an arbitrary place
            PdfPage newFirstPage = pdf.InsertPage(0);
            newFirstPage.Canvas.DrawString("New first page");

            // This call is an equivalent of AddPage method
            PdfPage lastPage = pdf.InsertPage(pdf.PageCount);
            lastPage.Canvas.DrawString("Last page");

            string pathToFile = "AddPages.pdf";
            pdf.Save(pathToFile);
            pdf.Dispose();

            Process.Start(pathToFile);
        }
    }
}
CopyVB.NET
Imports System.Diagnostics
Imports System.IO

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class AddPages
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.


            Dim pdf As New PdfDocument()

            ' There is already one page in PDF document after creation
            Dim firstPage As PdfPage = pdf.Pages(0)
            firstPage.Canvas.DrawString("This is the auto-created first page")

            ' AddPage method adds a new page to the end of the PDF document
            Dim secondPage As PdfPage = pdf.AddPage()
            secondPage.Canvas.DrawString("Second page")

            ' You can insert a new page in an arbitrary place
            Dim newFirstPage As PdfPage = pdf.InsertPage(0)
            newFirstPage.Canvas.DrawString("New first page")

            ' This call is an equivalent of AddPage method
            Dim lastPage As PdfPage = pdf.InsertPage(pdf.PageCount)
            lastPage.Canvas.DrawString("Last page")

            Dim pathToFile As String = "AddPages.pdf"
            pdf.Save(pathToFile)
            pdf.Dispose()

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace