Page Manipulation
Rotate pages, adjust page boxes (MediaBox, CropBox, BleedBox, TrimBox, ArtBox), and linearize documents for fast web viewing. These operations give you fine-grained control over how each page is displayed, printed, and trimmed.
Page Rotation
Set the rotation of any page to 0, 90, 180, or 270 degrees. The rotation is stored in the page dictionary and applied by viewers when displaying the page — the actual content stream is not rewritten.
PdfEditor.Open("scanned-document.pdf")
.SetPageRotation(pageIndex: 0, degrees: 90) // rotate first page 90 degrees clockwise
.Save("scanned-rotated.pdf");
Rotate Multiple Pages
using var inspector = PdfiumDocument.Load(File.ReadAllBytes("booklet.pdf"));
int pageCount = inspector.PageCount;
inspector.Dispose();
var editor = PdfEditor.Open("booklet.pdf");
// Rotate all even-numbered pages (0-indexed: pages 1, 3, 5, ...)
for (int i = 1; i < pageCount; i += 2)
editor.SetPageRotation(i, 180);
editor.Save("booklet-corrected.pdf");
Valid Rotation Values
| Degrees | Effect |
|---|---|
0 | No rotation (portrait default) |
90 | 90 degrees clockwise |
180 | Upside down |
270 | 90 degrees counter-clockwise (same as 270 clockwise) |
Only multiples of 90. PDF rotation is limited to 0, 90, 180, and 270. Passing any other value throws an ArgumentException. For arbitrary-angle rotation of content, use the layout engine's .Rotate() method during document generation.
Page Boxes
Every PDF page can have up to five rectangular boundary boxes that control different aspects of display, printing, and production. FolioPDF can read all five and write the two most commonly needed ones (MediaBox and CropBox).
Box Hierarchy
The boxes form a containment hierarchy. Each box must fall within its parent:
MediaBox (outermost — total page area)
└─ CropBox (visible area when displayed on screen)
├─ BleedBox (area for production bleed)
├─ TrimBox (final page size after trimming)
└─ ArtBox (extent of meaningful content)
| Box | Required? | Purpose |
|---|---|---|
| MediaBox | Yes | The full physical page area. Every page must have one. Defines the coordinate space for all content. |
| CropBox | No | The region displayed by viewers and used as the default clipping boundary. Defaults to MediaBox when absent. |
| BleedBox | No | The area to which content should extend to allow for trimming, folding, and binding in production. Typically 3–5mm larger than the TrimBox on each side. |
| TrimBox | No | The intended final page size after trimming. This is what the reader considers the "real" page size. |
| ArtBox | No | The extent of the page's meaningful content (artwork). Rarely used in practice. |
Setting the MediaBox
The MediaBox defines the total page area. Coordinates are (left, bottom, right, top) in PDF points:
PdfEditor.Open("document.pdf")
.SetMediaBox(
pageIndex: 0,
left: 0,
bottom: 0,
right: 612, // US Letter width
top: 792) // US Letter height
.Save("document-resized.pdf");
Setting the CropBox
The CropBox controls what viewers display. Use it to visually trim a page without removing content:
// Crop 50 points from each side of an A4 page
PdfEditor.Open("report.pdf")
.SetCropBox(
pageIndex: 0,
left: 50,
bottom: 50,
right: 545, // 595 - 50
top: 792) // 842 - 50
.Save("report-cropped.pdf");
CropBox vs content removal. Setting the CropBox does not delete content outside the visible area. It only hides it from display. The full content remains in the PDF and can be recovered by resetting the CropBox. For permanent content removal, use redaction.
Reading Page Boxes
Use the PdfiumDocument and PdfiumPage inspection API to read box values:
using var doc = PdfiumDocument.Load(File.ReadAllBytes("document.pdf"));
using var page = doc.GetPage(0);
// MediaBox is always present
var mediaBox = page.GetMediaBox();
Console.WriteLine($"MediaBox: {mediaBox.Left}, {mediaBox.Bottom}, " +
$"{mediaBox.Right}, {mediaBox.Top}");
Console.WriteLine($"Page size: {mediaBox.Width} x {mediaBox.Height} points");
// Optional boxes return null when not set
var cropBox = page.GetCropBox();
if (cropBox.HasValue)
Console.WriteLine($"CropBox: {cropBox.Value.Left}, {cropBox.Value.Bottom}, " +
$"{cropBox.Value.Right}, {cropBox.Value.Top}");
else
Console.WriteLine("CropBox: not set (defaults to MediaBox)");
var bleedBox = page.GetBleedBox();
var trimBox = page.GetTrimBox();
var artBox = page.GetArtBox();
Console.WriteLine($"BleedBox: {(bleedBox.HasValue ? bleedBox.ToString() : "not set")}");
Console.WriteLine($"TrimBox: {(trimBox.HasValue ? trimBox.ToString() : "not set")}");
Console.WriteLine($"ArtBox: {(artBox.HasValue ? artBox.ToString() : "not set")}");
Page Box Methods Summary
| Method | Returns | Description |
|---|---|---|
GetMediaBox() | RectangleF | Always present. Full page area. |
SetMediaBox(left, bottom, right, top) | void | Set the full page area. |
GetCropBox() | RectangleF? | Null if not explicitly set. |
SetCropBox(left, bottom, right, top) | void | Set the visible display area. |
GetBleedBox() | RectangleF? | Null if not set. Production bleed area. |
GetTrimBox() | RectangleF? | Null if not set. Final trimmed page size. |
GetArtBox() | RectangleF? | Null if not set. Meaningful content extent. |
Practical Example: Add Print Bleed
Extend the MediaBox by 9 points (approximately 3mm) on each side to create a bleed area, and set the TrimBox to the original page size. This is a standard pre-press setup:
using var inspector = PdfiumDocument.Load(File.ReadAllBytes("brochure.pdf"));
using var page = inspector.GetPage(0);
var media = page.GetMediaBox();
float bleed = 9; // ~3mm
var editor = PdfEditor.Open("brochure.pdf");
editor.SetMediaBox(0,
left: media.Left - bleed,
bottom: media.Bottom - bleed,
right: media.Right + bleed,
top: media.Top + bleed);
// The original page dimensions become the TrimBox
// (TrimBox can be set via the PdfiumPage API directly)
editor.Save("brochure-with-bleed.pdf");
Linearization
Linearization (also called "fast web view") restructures the PDF so that a viewer can begin rendering the first page as soon as the first portion of the file has been received, without waiting for the entire download. This is critical for PDFs served over HTTP.
PdfEditor.Open("large-report.pdf")
.Linearize()
.Save("large-report-web.pdf");
When to Linearize
| Scenario | Linearize? |
|---|---|
| PDF served via a web server or CDN | Yes |
| PDF embedded in a web application | Yes |
| PDF stored for archival (PDF/A) | Optional (helpful but not required) |
| PDF attached to email | No benefit (entire file is downloaded first) |
| PDF printed locally | No benefit |
Linearization and encryption. Linearization can be combined with encryption. Apply both in the same chain — qpdf handles the correct ordering internally.
Generate, Compress, and Linearize
A common production pipeline: generate a PDF, compress it, then linearize for web delivery:
PdfEditor.Create(doc =>
{
doc.Page(page =>
{
page.Size(PageSizes.A4);
page.Margin(50);
page.Content().Column(col =>
{
col.Item().Text("Product Catalogue").FontSize(28).Bold();
// ... catalogue content ...
});
});
})
.SetTitle("Product Catalogue 2026")
.Compress(new CompressionOptions { ImageQuality = 70 })
.Linearize()
.Save("catalogue-web.pdf");
Combining Operations
Page manipulation, rotation, box adjustments, and linearization can all be combined in a single editor chain:
var editor = PdfEditor.Open("scan-batch.pdf");
// Fix the orientation of the first three scanned pages
editor.SetPageRotation(0, 90);
editor.SetPageRotation(1, 90);
editor.SetPageRotation(2, 90);
// Crop white borders from the cover page
editor.SetCropBox(0,
left: 20, bottom: 20,
right: 575, top: 822);
// Add metadata
editor.SetTitle("Scanned Documents - March 2026");
// Linearize for web viewing
editor.Linearize();
editor.Save("scan-batch-processed.pdf");
Inspecting Page Properties
Read rotation and dimensions for all pages in a document:
using var doc = PdfiumDocument.Load(File.ReadAllBytes("mixed-layout.pdf"));
for (int i = 0; i < doc.PageCount; i++)
{
using var page = doc.GetPage(i);
var media = page.GetMediaBox();
Console.WriteLine($"Page {i + 1}:");
Console.WriteLine($" Size: {media.Width:F1} x {media.Height:F1} points " +
$"({media.Width / 72:F2} x {media.Height / 72:F2} inches)");
Console.WriteLine($" Orientation: {(media.Width > media.Height ? "Landscape" : "Portrait")}");
var crop = page.GetCropBox();
if (crop.HasValue)
{
Console.WriteLine($" CropBox: {crop.Value.Left:F1}, {crop.Value.Bottom:F1}, " +
$"{crop.Value.Right:F1}, {crop.Value.Top:F1}");
}
}
Common Page Sizes Reference
| Size | Width (pt) | Height (pt) | Width (mm) | Height (mm) |
|---|---|---|---|---|
| A3 | 842 | 1191 | 297 | 420 |
| A4 | 595 | 842 | 210 | 297 |
| A5 | 420 | 595 | 148 | 210 |
| US Letter | 612 | 792 | 216 | 279 |
| US Legal | 612 | 1008 | 216 | 356 |
| US Tabloid | 792 | 1224 | 279 | 432 |
Points to millimetres. 1 PDF point = 1/72 inch = 0.3528mm. To convert: mm = points * 25.4 / 72. To convert back: points = mm * 72 / 25.4.
Next Steps
- Drawing on Pages — draw text, lines, and shapes at precise coordinates
- Stamping & Watermarks — alignment-based stamps and watermarks
- Encryption — password-protect and restrict documents
- Compression — reduce file size for web and storage