---
title: "Read and write PDF page boxes"
canonical: https://www.graphicsmill.com/samples/Codecs/Pdf/PageBoxes_cs
product: "Graphics Mill"
source: "Codecs/Pdf/PageBoxes.cs"
---

# Read and write PDF page boxes

This sample shows how to define print production page boundaries (MediaBox, CropBox, BleedBox, TrimBox, and ArtBox) when writing a 300 DPI PDF, and how to read them back afterwards. It builds a bleed-aware business card layout with ` PdfWriter ` and ` PdfPage `, converting real-world sizes to pixels with ` UnitConverter `, then reopens the file with ` PdfReader ` to show that ` CropBox `, ` BleedBox `, ` TrimBox `, and ` ArtBox ` always come back in points regardless of the page's DPI.

## Instruction

Use the PDF API to lay out a print-ready page with bleed, trim, and safe-area boxes: convert real-world sizes to pixels with UnitConverter, set the page's CropBox, BleedBox, TrimBox, and ArtBox with PdfWriter and PdfPage while drawing the artwork, then reopen the file with PdfReader and print each box back to the console.

## Usings

```csharp
using System;
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Drawing;
```

## Code Snippet

```csharp
public static void Run()
{
    const string pdfPath = "PageBoxes.pdf";
    const float dpi = 300; // A common print resolution.

    // A typical print production layout: the sheet includes bleed, TrimBox is the finished
    // card size, and ArtBox is a safety margin the design shouldn't cross. PdfWriter treats
    // all these box values as pixels at the page's own DPI, so real-world sizes are converted
    // through UnitConverter rather than hardcoded as pixel counts.
    var bleed = UnitConverter.ConvertUnitsToPixels(dpi, 0.125f, Unit.Inch);       // 1/8" bleed
    var safeMargin = UnitConverter.ConvertUnitsToPixels(dpi, 0.1875f, Unit.Inch); // 3/16" safety margin
    var trimWidth = UnitConverter.ConvertUnitsToPixels(dpi, 3.5f, Unit.Inch);     // Business card width
    var trimHeight = UnitConverter.ConvertUnitsToPixels(dpi, 2f, Unit.Inch);      // Business card height
    var hairlineWidth = UnitConverter.ConvertUnitsToPixels(dpi, 0.5f, Unit.Point); // 0.5pt trim guide

    var sheetBox = new System.Drawing.RectangleF(0, 0, trimWidth + (2 * bleed), trimHeight + (2 * bleed));
    var trimBox = new System.Drawing.RectangleF(bleed, bleed, trimWidth, trimHeight);
    var artBox = new System.Drawing.RectangleF(
        trimBox.Left + safeMargin,
        trimBox.Top + safeMargin,
        trimBox.Width - (2 * safeMargin),
        trimBox.Height - (2 * safeMargin));

    using (var writer = new PdfWriter(pdfPath))
    {
        // MediaBox comes from the page size passed here; CropBox and BleedBox share it because
        // the whole sheet, bleed included, is meant to be printed and cut down to TrimBox.
        var page = new PdfPage(sheetBox.Width, sheetBox.Height, dpi, dpi)
        {
            CropBox = sheetBox,
            BleedBox = sheetBox,
            TrimBox = trimBox,
            ArtBox = artBox,
        };

        writer.AddPage(page);

        using (var gr = writer.GetGraphics())
        {
            // Background runs to the edge of the bleed area so no white sliver appears after trimming.
            gr.FillRectangle(new SolidBrush(RgbColor.MidnightBlue), sheetBox.X, sheetBox.Y, sheetBox.Width, sheetBox.Height);

            // A hairline at the trim box shows where the sheet will actually be cut.
            gr.DrawRectangle(new Pen(RgbColor.White, hairlineWidth), trimBox.X, trimBox.Y, trimBox.Width, trimBox.Height);

            // Keep the company name inside the art box, clear of the trim line.
            var font = gr.CreateFont("Arial", 14f);
            var name = new PlainText("Northwind Creative LLC", font, new SolidBrush(RgbColor.White), artBox.Left, artBox.Top + 20f, TextAlignment.Left);
            gr.DrawText(name);
        }
    }

    // Read the same file back. Unlike PdfWriter, PdfReader always reports CropBox, BleedBox,
    // TrimBox, and ArtBox in points regardless of the page's DPI; MediaBox alone is left in the
    // PDF's own bottom-left-origin coordinate system rather than GraphicsMill's.
    using (var reader = new PdfReader(pdfPath))
    {
        var page = reader.Frames[0];

        Console.WriteLine("MediaBox (points): {0}", page.MediaBox);
        Console.WriteLine("CropBox  (points): {0}", page.CropBox);
        Console.WriteLine("BleedBox (points): {0}", page.BleedBox);
        Console.WriteLine("TrimBox  (points): {0}", page.TrimBox);
        Console.WriteLine("ArtBox   (points): {0}", page.ArtBox);
        Console.WriteLine();

        // Converting every box back to inches makes it easy to compare against the physical
        // sizes (3.5x2" trim, 1/8" bleed, 3/16" safe margin) the page was designed with.
        PrintBoxSizeInInches("MediaBox", page.MediaBox, reader.DpiX);
        PrintBoxSizeInInches("CropBox", page.CropBox, reader.DpiX);
        PrintBoxSizeInInches("BleedBox", page.BleedBox, reader.DpiX);
        PrintBoxSizeInInches("TrimBox", page.TrimBox, reader.DpiX);
        PrintBoxSizeInInches("ArtBox", page.ArtBox, reader.DpiX);
    }
}

private static void PrintBoxSizeInInches(string boxName, System.Drawing.RectangleF boxInPoints, float resolution)
{
    var widthInch = UnitConverter.ConvertUnitsToUnits(resolution, boxInPoints.Width, Unit.Point, Unit.Inch);
    var heightInch = UnitConverter.ConvertUnitsToUnits(resolution, boxInPoints.Height, Unit.Point, Unit.Inch);

    Console.WriteLine("{0} size in inches: {1} x {2}", boxName, widthInch, heightInch);
}
```

## Explanation

The code:

- Converts real-world print sizes (bleed, safe margin, trim width and height) from inches to pixels at 300 DPI using ` UnitConverter.ConvertUnitsToPixels `.
- Creates a ` PdfPage ` sized to the full bleed area and assigns its ` CropBox `, ` BleedBox `, ` TrimBox `, and ` ArtBox `.
- Adds the page to a ` PdfWriter ` and fills the whole bleed area with a background color so no white edge appears after trimming.
- Draws a hairline at the trim box and the company name inside the art box, using ` Graphics.DrawRectangle ` and ` Graphics.DrawText `.
- Reopens the saved file with ` PdfReader ` and reads the first page's ` MediaBox `, ` CropBox `, ` BleedBox `, ` TrimBox `, and ` ArtBox `.
- Converts the read-back ` TrimBox ` from points to pixels with ` UnitConverter.ConvertUnitsToPixels ` to confirm it matches the size that was written.
- Converts every box from points to inches with a small ` PrintBoxSizeInInches ` helper, to compare against the physical sizes the page was designed with.

## Tags

- [Advanced](<https://www.graphicsmill.com/samples/tags/advanced_md>): Demonstrating advanced usage scenarios
- [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

- [PageBoxes.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/PageBoxes_pdf>) (download)
- [PageBoxes.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/PageBoxes_png>) (preview)
- [PageBoxes.txt](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/PageBoxes_txt>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.ImageReader.DpiX](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_DpiX.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfFrame.ArtBox](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfFrame_ArtBox.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfFrame.BleedBox](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfFrame_BleedBox.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfFrame.CropBox](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfFrame_CropBox.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfFrame.MediaBox](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfFrame_MediaBox.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfFrame.TrimBox](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfFrame_TrimBox.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfPage](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfPage.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfPage.ArtBox](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfPage_ArtBox.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfPage.BleedBox](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfPage_BleedBox.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfPage.CropBox](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfPage_CropBox.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfPage.TrimBox](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfPage_TrimBox.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(PdfPage)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_AddPage_1_8a7e379c.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.CreateFont(String,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_CreateFont_2_1dbfa9fa.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawRectangle(Pen,Single,Single,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawRectangle_5_b3708ac1.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.Pen](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_Pen.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>)
- [Aurigma.GraphicsMill.UnitConverter.ConvertUnitsToUnits(Single,Single,Unit,Unit)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_UnitConverter_ConvertUnitsToUnits_4_11d3dc01.htm.md>)
