How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

Using C# PDF Page Resize Library
C#: How to get, scale, resize pdf page size (width, height) using c# code


C# Demo Code to change pdf page size (width, height) using c#





In this tutorial, you learn about PDF page size related knowledge and how to resize a PDF page using C# in ASP.NET MVC Web, Windows applications.

  • PDF Page Boundaries
  • Read page boundaries values
  • Change PDF page size

How to get, set a PDF page size using C#

  1. Download XDoc.PDF Page Size C# library
  2. Install C# library to get, change PDF page size
  3. Step by Step Tutorial














PDF Page Boundaries


A PDF page is bounded by five different Page Boxes. Each page box has a special purpose.

XDoc.PDF C# library has provided all five boxes read and write API. If you want to adjust the visible page area, you can view the PDF page crop in C#.

  • MediaBox: Target paper size, for example, finished size of printed document. A rectangle defines the boundaries of the physical medium on which the page is intended to be displayed or printed.
  • CropBox: Page visible in the viewer, for example what the user sees on the screen. A rectangle defines the visible region of default user space. When the page is displayed or printed, its contents are to be clipped (cropped) to this rectangle and then imposed on the output medium in some implementation-defined manner. CropBox area should not be larger than MediaBox.
  • BleedBox: Used only for printing. A rectangle defines the region to which the contents of the page should be clipped when output in a production environment.
  • TrimBox: Used only for printing. A rectangle defines the intended dimensions of the finished page after trimming.
  • ArtBox: Used only for printing. A rectangle defines the extent of the page's meaningful content (including potential white space) as intended by the page's creator.




C# PDF page library also provides how to delete a page from a pdf in c#, c# pdf add new page, c# pdf add background, c# extract pdf page as image, c# rotate pdf pages.







Read, get a PDF page size (Page Boundaries) in C#


The C# code below explains how to read PDF page boundary values for MediaBox, CropBox, BleedBox, TrimBox, and ArtBox.



String inputFilePath = @"C:\1.pdf";

//  Open file and get the target page
PDFDocument doc = new PDFDocument(inputFilePath);
PDFPage page = (PDFPage)doc.GetPage(0);

//  Get media box of the target page.
float[] bbox = page.GetPageBoundary(PDFPageBoundaryType.MediaBox);
Console.WriteLine("Media Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
//  Get crop box of the target page.
bbox = page.GetPageBoundary(PDFPageBoundaryType.CropBox);
if (bbox != null)
    Console.WriteLine("Crop Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
else
    Console.WriteLine("No Crop Box");
//  Get bleed box of the target page.
bbox = page.GetPageBoundary(PDFPageBoundaryType.BleedBox);
if (bbox != null)
    Console.WriteLine("Bleed Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
else
    Console.WriteLine("No Bleed Box");
//  Get trim box of the target page.
bbox = page.GetPageBoundary(PDFPageBoundaryType.TrimBox);
if (bbox != null)
    Console.WriteLine("Trim Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
else
    Console.WriteLine("No Trim Box");
//  Get art box of the target page.
bbox = page.GetPageBoundary(PDFPageBoundaryType.ArtBox);
if (bbox != null)
    Console.WriteLine("Art Box: llx={0}, lly={1}, urx={2}, ury={3}", bbox[0], bbox[1], bbox[2], bbox[3]);
else
    Console.WriteLine("No Art Box");






How to resizing a PDF page in C#


There is a function for setting all five page boxes, PDFPage.SetPageBoundary().

There is one thing to notice here. The CropBox area should not be larger than MediaBox. Otherwise some area will not be printed or visible.

The C# source code below shows how to set page boundaries for BleedBox, TrimBox, and ArtBox.



String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file and get the target page
PDFDocument doc = new PDFDocument(inputFilePath);
PDFPage page = (PDFPage)doc.GetPage(0);

float[] bbox = page.GetPageBoundary(PDFPageBoundaryType.MediaBox);
//  Add (or update) bleed box.
page.SetPageBoundary(PDFPageBoundaryType.BleedBox, bbox[0] + 20, bbox[1] + 20, bbox[2] - 50, bbox[3] - 60);
//  Add (or update) trim box.
page.SetPageBoundary(PDFPageBoundaryType.TrimBox, bbox[0] + 60, bbox[1] + 40, bbox[2] - 100, bbox[3] - 160);
//  Add (or update) art box.
page.SetPageBoundary(PDFPageBoundaryType.ArtBox, bbox[0] + 90, bbox[1] + 50, bbox[2] - 150, bbox[3] - 260);

//  Save file
doc.Save(outputFilePath);