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 the 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 the 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

See also


Next steps

GemBox.Pdf is a .NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from .NET applications in a simple and efficient way.

Download Buy