---
title: "Add and remove pages in an existing PDF"
canonical: https://www.graphicsmill.com/samples/Codecs/Pdf/AddAndRemovePages_cs
product: "Graphics Mill"
source: "Codecs/Pdf/AddAndRemovePages.cs"
---

# Add and remove pages in an existing PDF

This sample shows how to insert and delete pages of an existing PDF document. Since PDF pages can't be edited in place, it reopens the source file with ` PdfReader ` and rebuilds it page by page with ` PdfWriter `: copying each page's content with ` GetContent ` and ` Graphics.DrawContainer `, skipping the page to remove, and adding a brand-new page where one should be inserted.

## Instruction

Use the PDF API to add and remove pages in an existing PDF document: reopen the file with PdfReader, then rewrite it page by page with PdfWriter, copying each page's content with GetContent and Graphics.DrawContainer, skipping any pages to remove and adding new pages wherever they should be inserted.

## Usings

```csharp
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Drawing;
```

## Code Snippet

```csharp
System.Action<Graphics, float, float, RgbColor, string> drawLabeledPage = (gr, width, height, color, label) =>
{
    gr.FillRectangle(new SolidBrush(color), 0, 0, width, height);

    var font = gr.CreateFont("Arial", 28f);
    var text = new PlainText(label, font, new SolidBrush(RgbColor.White), width / 2f, (height / 2f) - 20f, TextAlignment.Center);
    gr.DrawText(text);
};

const string originalPath = "Original.pdf";
const string editedPath = "EditedDocument.pdf";
const float dpi = 150f;
var pageWidth = UnitConverter.ConvertUnitsToPixels(dpi, 5f, Unit.Inch);
var pageHeight = UnitConverter.ConvertUnitsToPixels(dpi, 3f, Unit.Inch);

// Build a simple 4-page starting document to edit afterwards.
var pageColors = new[] { RgbColor.SteelBlue, RgbColor.DarkOrange, RgbColor.ForestGreen, RgbColor.Firebrick };
using (var writer = new PdfWriter(originalPath))
using (var gr = writer.GetGraphics())
{
    for (var i = 0; i < pageColors.Length; i++)
    {
        writer.AddPage(pageWidth, pageHeight, dpi, dpi);
        drawLabeledPage(gr, pageWidth, pageHeight, pageColors[i], string.Format("Page {0}", i + 1));
    }
}

// PDF pages can't be edited in place, so the document is rebuilt page by page instead:
// the page to remove is simply never copied, and the new page is written in its place.
const int pageIndexToRemove = 1; // Zero-based; this is "Page 2".

using (var reader = new PdfReader(originalPath))
using (var writer = new PdfWriter(editedPath))
using (var gr = writer.GetGraphics())
{
    for (var i = 0; i < reader.Frames.Count; i++)
    {
        if (i != pageIndexToRemove)
        {
            using (var pageContent = reader.Frames[i].GetContent())
            {
                writer.AddPage(pageContent.Width, pageContent.Height, pageContent.DpiX, pageContent.DpiY);
                gr.DrawContainer(pageContent, 0, 0);
            }
        }

        if (i == 0)
        {
            // Insert a brand-new page right after the first one.
            writer.AddPage(pageWidth, pageHeight, dpi, dpi);
            drawLabeledPage(gr, pageWidth, pageHeight, RgbColor.Gold, "Inserted page");
        }
    }
}
```

## Explanation

The code:

- Builds a starting 4-page document, "Original.pdf", with a differently colored, labeled page.
- Reopens it with ` PdfReader ` and creates a new ` PdfWriter ` for the edited document.
- Walks the source pages by index, skipping the one page that should be removed.
- Copies every other page across by getting its content with ` GetContent ` and drawing it with ` Graphics.DrawContainer `.
- Right after the first page, adds and draws one brand-new page that didn't exist in the source file.
- Produces "EditedDocument.pdf", which has as many pages as the original even though one was removed and one was inserted.

## 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

## Output

- [Original.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Original_pdf>) (download)
- [EditedDocument.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/EditedDocument_pdf>) (download)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.FrameCollectionBase.Count](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_FrameCollectionBase_Count.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](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_Graphics.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.CreateFont(String,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_CreateFont_2_1dbfa9fa.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.Graphics.DrawText(Text)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawText_1_f576d03f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.FillRectangle(Brush,Single,Single,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_FillRectangle_5_2d30ad96.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>)
- [Aurigma.GraphicsMill.Drawing.PlainText](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_PlainText.htm.md>)
- [Aurigma.GraphicsMill.Drawing.SolidBrush](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_SolidBrush.htm.md>)
- [Aurigma.GraphicsMill.Drawing.TextAlignment](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_TextAlignment.htm.md>)
- [Aurigma.GraphicsMill.Unit](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Unit.htm.md>)
- [Aurigma.GraphicsMill.UnitConverter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_UnitConverter.htm.md>)
- [Aurigma.GraphicsMill.UnitConverter.ConvertUnitsToPixels(Single,Single,Unit)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_UnitConverter_ConvertUnitsToPixels_3_1df1b0a2.htm.md>)
