Clone or import pages between PDF documents in C# and VB.NET

With GemBox.Pdf, you can clone or import pages (and even page trees) between PDF documents in your C# or VB.NET application.

It’s possible to duplicate an entire PdfDocument, using the PdfDocument.Clone() method, or just a specific page using the PdfPages.AddClone() method. For more information about cloning in GemBox.Pdf, see the Document Structure help page.

The following example shows how you can clone pages from another PDF document.

PDF pages cloned with GemBox.Pdf C#/VB.NET library
Screenshot of PDF pages cloned with GemBox.Pdf library
Upload your file (Drag file here)
using System;
using GemBox.Pdf;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = PdfDocument.Load("%InputFileName%"))
        {
            int pageCount = %PagesCount%;

            // Load a source document.
            using (var source = PdfDocument.Load("%#LoremIpsum.pdf%"))
            {
                // Get the number of pages to clone.
                int cloneCount = Math.Min(pageCount, source.Pages.Count);

                // Clone the requested number of pages from the source document
                // and add them to the destination document.
                for (int i = 0; i < cloneCount; i++)
                    document.Pages.AddClone(source.Pages[i]);
            }

            document.Save("Cloning.pdf");
        }
    }
}
Imports System
Imports GemBox.Pdf

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = PdfDocument.Load("%InputFileName%")

            Dim pageCount As Integer = %PagesCount%

            ' Load a source document.
            Using source = PdfDocument.Load("%#LoremIpsum.pdf%")

                ' Get the number of pages to clone.
                Dim cloneCount = Math.Min(pageCount, source.Pages.Count)

                ' Clone the requested number of pages from the source document 
                ' and add them to the destination document.
                For i As Integer = 0 To cloneCount - 1
                    document.Pages.AddClone(source.Pages(i))
                Next
            End Using

            document.Save("Cloning.pdf")
        End Using
    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Pdf yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.