---
title: "Combine multiple PDF files into one document"
canonical: https://www.graphicsmill.com/samples/Codecs/Pdf/MergePages_cs
product: "Graphics Mill"
source: "Codecs/Pdf/MergePages.cs"
---

# Combine multiple PDF files into one document

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.

## Instruction

Use the PDF API to combine several existing PDF files into a single multi-page document: read each source file with PdfReader, and for every page, copy its content into a new file with PdfWriter, keeping the original page size and resolution.

## Usings

```csharp
using System.Linq;
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Drawing;
```

## Code Snippet

```csharp
// 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);
                }
            }
        }
    }
}
```

## Explanation

The code:

- Lists the source PDF files to combine, in the order their pages should appear in the merged document.
- Creates a single ` PdfWriter ` for the combined output file, "MergedDocument.pdf".
- Opens each source file in turn with ` PdfReader `.
- Iterates over each source file's pages (` PdfFrame ` objects) and gets its vector content as a ` GraphicsContainer ` via ` GetContent `.
- Adds a page to the output sized to match the source page's width, height, and resolution.
- Draws the source page's content onto the new page with ` Graphics.DrawContainer `.
- Repeats for every page of every source file, producing one combined multi-page PDF.

## Tags

- [Graphics Container](<https://www.graphicsmill.com/samples/tags/container_md>): Using graphics containers for grouped items
- [PDF](<https://www.graphicsmill.com/samples/tags/pdf_md>): Importing and exporting PDF documents
- [Read](<https://www.graphicsmill.com/samples/tags/read_md>): Reading images with codecs
- [Write](<https://www.graphicsmill.com/samples/tags/write_md>): Writing images with codecs

## Input

- [GraphicsContainer.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/GraphicsContainer_pdf>) (download)
- [Seal.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Seal_pdf>) (download)
- [business-card-front\_template.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/business-card-front_template_pdf>) (download)

## Output

- [MergedDocument.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/MergedDocument_pdf>) (download)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.PdfFrame](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfFrame.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfFrame.GetContent](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfFrame_GetContent.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfReader.Frames](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfReader_Frames.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.AddPage(Single,Single,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_AddPage_4_37f6c741.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.GetGraphics](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_GetGraphics.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawContainer(GraphicsContainer,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawContainer_3_098aa7ce.htm.md>)
- [Aurigma.GraphicsMill.Drawing.GraphicsContainer.DpiX](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_GraphicsContainer_DpiX.htm.md>)
- [Aurigma.GraphicsMill.Drawing.GraphicsContainer.DpiY](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_GraphicsContainer_DpiY.htm.md>)
