C#: Copy Pages in PDF

Copying PDF pages facilitates better organization of information. By copying pages that contain important sections and then compiling them into a new document, you can bring together relevant content from different sources to create a cohesive resource that is easy to navigate. In this article, you will learn how to copy pages in PDF in C# using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Copy Pages within the Same PDF in C#

To duplicate PDF pages, you can first create template based on a specified page in PDF, and then draw the template on a newly added page through the PdfPageBase.Canvas.DrawTemplate() method. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified page using PdfDocument.Pages[] property.
  • Get the size of the page using PdfPageBase.Size property.
  • Create a template based on the page using PdfPageBase.CreateTemplate() method.
  • Add a new page of the same size at the end using PdfDocument.Pages.Add(SizeF size, PdfMargins margins) method. Or you can insert a new page of the same size at a specified location using PdfDocument.Pages.Insert(int index, SizeF size, PdfMargins margins) method.
  • Draw template on the newly added page using PdfPageBase.Canvas.DrawTemplate(PdfTemplate template, PointF location) method.
  • Save the result file using PdfDocument.SaveToFile() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace DuplicatePage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file
            pdf.LoadFromFile("Butterflies.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Get the size of the page
            SizeF size = page.Size;

            //Create a template based on the page
            PdfTemplate template = page.CreateTemplate();

            //Add a new page the same size as the first page
            page = pdf.Pages.Add(size, new PdfMargins(0));
            //Insert a new page at the specified location
            //page = pdf.Pages.Insert(1, size, new PdfMargins(0));

            //Draw the template on the newly added page
            page.Canvas.DrawTemplate(template, new PointF(0, 0));

            //Save the PDF file
            pdf.SaveToFile("CopyPDFPages.pdf");
        }
    }
}

C#: Copy Pages in PDF

Copy Pages from One PDF to Another in C#

Spire.PDF for .NET also allows you to load two PDF files, create templates based on the pages in one PDF file, and then draw them onto the pages in another PDF file. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load two PDF files using PdfDocument.LoadFromFile() method.
  • Get a specified page in the first PDF using PdfDocument.Pages[] property.
  • Get the size of the page using PdfPageBase.Size property.
  • Create a template based on the page using PdfPageBase.CreateTemplate() method.
  • Insert a new page of the same size at a specified location in the second PDF using PdfDocument.Pages.Insert(int index, SizeF size, PdfMargins margins) method. Or you can add a new page of the same size at the end of the second PDF using PdfDocument.Pages.Add(SizeF size, PdfMargins margins) method.
  • Draw template on the newly added page using PdfPageBase.Canvas.DrawTemplate(PdfTemplate template, PointF location) method.
  • Save the result file using PdfDocument.SaveToFile() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace CopyPageToAnother
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the first PDF file
            PdfDocument pdf1 = new PdfDocument();
            pdf1.LoadFromFile("Butterflies.pdf");

            //Load the second PDF file
            PdfDocument pdf2 = new PdfDocument();
            pdf2.LoadFromFile("SamplePDF.pdf");

            //Get the first page in the first PDF file
            PdfPageBase page = pdf1.Pages[0];

            //Get the size of the page
            SizeF size = page.Size;

            //Create a template based on the page
            PdfTemplate template = page.CreateTemplate();

            //Insert a new page at a specified location in the second PDF file
            PdfPageBase newPage = pdf2.Pages.Insert(0, size, new PdfMargins(0));

            //Add a new page at the end of the second PDF file
            //PdfPageBase newPage = pdf2.Pages.Add(size, new PdfMargins(0));

            //Draw the template on the newly added page
            newPage.Canvas.DrawTemplate(template, new PointF(0, 0));

            //Save the result file
            pdf2.SaveToFile("CopyPagesToAnotherPDF.pdf");
        }
    }
}

C#: Copy Pages in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.