How to Copy a Page within a PDF File or between PDF Files in WPF

In some cases, we need to copy one or more pages of a pdf file, while copy pdf pages can be classified into two categories: copy pages within a pdf file and copy pages between pdf files. With the help of Spire.PDF, we can easily achieve this task programmatically instead of using Adobe Acrobat and dragging the page to copy it manually.

This article will demonstrate how to copy a page within a pdf file or between pdf files in WPF using Spire.PDF for WPF.

Before using the code, please add the following namespace first:

using System.Drawing;
using System.Windows;
using Spire.Pdf;
using Spire.Pdf.Graphics;

Copy Page within a PDF File

Step 1: Initialize a new instance of PdfDocument class and load the sample pdf file.

PdfDocument doc1 = new PdfDocument();
doc1.LoadFromFile("Stories.pdf");

Step 2: Get the first page of the pdf file, then get its page size and call CreateTemplate() method to create a new pdf template based on the first page.

PdfPageBase page = doc1.Pages[0];
SizeF size = page.Size;
PdfTemplate template = page.CreateTemplate();

Step 3: Copy the first page within the pdf file.

Add a new page that is the same size as the first page to the pdf file, draw the template to the new page by invoking DrawTemplate(PdfTemplate template, PointF location) method.

page = doc1.Pages.Add(size, new PdfMargins(0,0));
page.Canvas.DrawTemplate(template,new PointF(0,0));

Step 4: Save and launch the file.

doc1.SaveToFile("copyWithin.pdf");
System.Diagnostics.Process.Start("copyWithin.pdf");

Effective Screenshot:

How to Copy a Page within a PDF File or between PDF Files in WPF

Copy Page between PDF Files

Step 1: Initialize a new instance of PdfDocument class named doc1 and load the first pdf file.

PdfDocument doc1 = new PdfDocument();
doc1.LoadFromFile("Stories.pdf");

Step 2: Initialize a new instance of PdfDocument class named doc2 and load the second pdf file.

PdfDocument doc2 = new PdfDocument();
doc2.LoadFromFile("Instruction.pdf");

Step 3: Get the first page of doc1, then get its page size and create a new template based on the first page.

PdfPageBase page = doc1.Pages[0];
SizeF size = page.Size;
PdfTemplate template = page.CreateTemplate();

Step 4: Copy the first page from doc1 to doc2.

Invoking Insert(int index, SizeF size, PdfMargins margins) method to insert a new page that is the same size as the first page to the specified location of doc2, next draw the template to the new page.

doc2.Pages.Insert(1, size, new PdfMargins(0,0));          
doc2.Pages[1].Canvas.DrawTemplate(template,new PointF(0,0));

If you want to copy the page to doc2 as its last page, please use the following code to add a new page to the end of doc2, then draw the template to the new page.

doc2.Pages.Add(size, new PdfMargins(0, 0));

Step 5: Save and launch the file.

doc2.SaveToFile("copyBetween.pdf");
System.Diagnostics.Process.Start("copyBetween.pdf");

Effective Screenshot:

How to Copy a Page within a PDF File or between PDF Files in WPF

Full codes:

Copy page within a pdf file:

private void button1_Click(object sender, RoutedEventArgs e)
{
    PdfDocument doc1 = new PdfDocument();
    doc1.LoadFromFile("Stories.pdf");

    PdfPageBase page = doc1.Pages[0];
    SizeF size = page.Size;
    PdfTemplate template = page.CreateTemplate();
            
    page = doc1.Pages.Add(size, new PdfMargins(0,0));
    page.Canvas.DrawTemplate(template, new PointF(0,0));

    doc1.SaveToFile("copyWithin.pdf");
    System.Diagnostics.Process.Start("copyWithin.pdf");
}

Copy page between pdf files:

private void button1_Click(object sender, RoutedEventArgs e)
{
    PdfDocument doc1 = new PdfDocument();
    doc1.LoadFromFile("Stories.pdf");
            
    PdfDocument doc2 = new PdfDocument();
    doc2.LoadFromFile("Instruction.pdf");

    PdfPageBase page = doc1.Pages[0];
    SizeF size = page.Size;
    PdfTemplate template = page.CreateTemplate();
             
doc2.Pages.Insert(1, size, new PdfMargins(0,0));           
    doc2.Pages[1].Canvas.DrawTemplate(template, new PointF(0,0));

    doc2.SaveToFile("copyBetween.pdf");
    System.Diagnostics.Process.Start("copyBetween.pdf");
}