Combine multiple PDF files into one document

PDF Read Write Graphics Container

This sample shows how to combine the pages of several existing PDF files into a single multi-page PDF document. It opens each source file with PdfReader, copies every page's vector content into a new document with PdfWriter and Graphics.DrawContainer, preserving each source page's own size and resolution.

Сode Snippet

// The pages of these files will end up in the merged document in this order.
var sourceFiles = new[]
{
    "GraphicsContainer.pdf",
    "Seal.pdf",
    "business-card-front_template.pdf",
};

using (var writer = new PdfWriter("MergedDocument.pdf"))
using (var gr = writer.GetGraphics())
{
    foreach (var sourceFile in sourceFiles)
    {
        using (var reader = new PdfReader(sourceFile))
        {
            // A source file can have more than one page; copy all of them.
            foreach (var frame in reader.Frames.Cast<PdfFrame>())
            {
                using (var pageContent = frame.GetContent())
                {
                    // Keep each source page's own size and resolution in the merged document.
                    writer.AddPage(pageContent.Width, pageContent.Height, pageContent.DpiX, pageContent.DpiY);

                    gr.DrawContainer(pageContent, 0, 0);
                }
            }
        }
    }
}

Input

GraphicsContainer.pdf

Download

Seal.pdf

Download

business-card-front_template.pdf

Download

Output

MergedDocument.pdf

Download

For AI-assisted development: Download Graphics Mill Code Samples XML Catalog