Get or Retrieve the Page Size of a PDF in C#

Andrew Wilson
2 min readJan 16, 2025

--

Knowing the page size of a PDF document is important as accurate page size information helps ensure that the document appears correctly when printed or displayed on different devices. For example, a document with a page size of 11 x 17 inches (tabloid size) will not fit on standard letter-sized paper without being resized or cropped. In this article, you will learn how to get or retrieve the PDF page size with C# using a free NET library.

Install the Free Library

To accomplish the task, we need the free library — Free Spire.PDF for .NET. It can be installed directly via NuGet, or we can also download the product package via the link below and unzip it to reference the dlls manually. (Be aware that the free .NET library has certain page limits).

https://www.e-iceblue.com/Introduce/free-pdf-component.html

Get or Retrieve PDF Page Size in C#

  1. Load a PDF file using the LoadFromFile() method of the PdfDocument class.
  2. Get a specified page in the PDF file.

3. Get the page height through the PdfPageBase.Size.Height property.

4. Get the page width through the PdfPageBase.Size.Width property.

5. Output the result.

📌 Note: The default unit of measure for page height and width is points. If you want to convert it to other units, you can create an instance of the PdfUnitConvertor class and use its ConvertUnits(float value, PdfGraphicsUnit from, PdfGraphicsUnit to) method to convert points to inches, centimeters, millimeters, and so on.

Sample C# code:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;

namespace GetPDFPageSize
{
class Program
{
static void Main(string[] args)
{
//Load a PDF file
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Test.pdf");

//Get the first page
PdfPageBase page = pdf.Pages[0];

//Get the width and height of the page in "point"
float pointWidth = page.Size.Width;
float pointHeight = page.Size.Height;

//Create PdfUnitConvertor to convert the unit
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

//Convert units from points to inches
float inchWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);
float inchHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);

//Convert units from points to centimeters
float centimeterWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
float centimeterHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);

//Output the result
Console.WriteLine("Page width: " + pointWidth + "pt, Page height: " + pointHeight + "pt");
Console.WriteLine("Page width: " + inchWidth + "inch, Page height: " + inchHeight + "inch");
Console.WriteLine("Page width: " + centimeterWidth + "cm, height: " + centimeterHeight + "cm");
}
}
}

Output result:

C# get the size of a PDF page

--

--

Andrew Wilson
Andrew Wilson

Written by Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.

No responses yet