C#: Get Page Size, Orientation and Rotation of PDF

2024-08-26 01:00:17 Written by  support iceblue
Rate this item
(0 votes)

In some cases, checking the size, orientation, and rotation of PDF pages can be part of the quality control process. For example, before publishing or distributing a document, you might need to verify this information to ensure that all pages in the document are correctly presented. In this article, you will learn how to get PDF page size, orientation and rotation angle in C# using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF 

Get PDF Page Size in C#

Spire.PDF for .NET offers the PdfPageBase.Size.Width and PdfPageBase.Size.Height properties to get the width and height of a PDF page in points. If you want to convert the default unit of measure to other units, you can use the PdfUnitConvertor class. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified page using PdfDocument.Pages[] property.
  • Get the width and height of the PDF page using PdfPageBase.Size.Width and PdfPageBase.Size.Height properties.
  • Create a PdfUnitConvertor instance, and then convert the size units from points to other units of measure using PdfUnitConvertor.ConvertUnits() method.
  • Add the page size information to a StringBuilder instance, and then save the result to a TXT file.
  • C#
using System.Text;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace GetPDFPageSize
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file from disk
            pdf.LoadFromFile("SamplePDF.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 size units from points to pixels
            float pixelWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
            float pixelHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);

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

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

            //Create a StringBuilder instance 
            StringBuilder content = new StringBuilder();

            //Add the page size information to the StringBuilder instance 
            content.AppendLine("The page size of the file in points is (width: " + pointWidth + "pt, height: " + pointHeight + "pt).");
            content.AppendLine("The page size of the file in pixels is (width: " + pixelWidth + "pixel, height: " + pixelHeight + "pixel).");
            content.AppendLine("The page size of the file in inches is (width: " + inchWidth + "inch, height: " + inchHeight + "inch).");
            content.AppendLine("The page size of the file in centimeters is (width: " + centimeterWidth + "cm, height: " + centimeterHeight + "cm).");

            //Save to a txt file
            File.WriteAllText("GetPageSize.txt", content.ToString());
        }
    }
}

C#: Get Page Size, Orientation and Rotation of PDF

Detect PDF Page Orientation in C#

To detect the orientation of a PDF page, you can compare the width and height of the page. If the page width is greater than the height, then the page orientation is landscape, otherwise it is portrait. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified page using PdfDocument.Pages[] property.
  • Get the width and height of the PDF page using PdfPageBase.Size.Width and PdfPageBase.Size.Height properties.
  • Compare the values of page width and height to detect the page orientation.
  • Output the result using Console.WriteLine() method.
  • C#
using Spire.Pdf;

namespace GetPDFPageOrientation
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file from disk
            pdf.LoadFromFile("SamplePDF.pdf");

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

            //Get the width and height of the page
            float width = page.Size.Width;
            float height = page.Size.Height;

            //Compare the values of page width and height 
            if (width > height)
            {
                Console.WriteLine("The page orientation is Landscape.");
            }

            else
            {
                Console.WriteLine("The page orientation is Portrait.");
            }
        }
    }
}

C#: Get Page Size, Orientation and Rotation of PDF

Detect PDF Page Rotation Angle in C#

The rotation angle of a PDF page can be obtained through the PdfPageBase.Rotation property. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified page using PdfDocument.Pages[] property.
  • Get the rotation angle of the page using PdfPageBase.Rotation property, and then convert it to text string.
  • Output the result using Console.WriteLine() method.
  • C#
using Spire.Pdf;

namespace GetPDFPageRotationAngle
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file from disk
            pdf.LoadFromFile("E:\\PythonPDF\\Sample.pdf");

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

            //Get the rotation angle of the current page
            PdfPageRotateAngle rotationAngle = page.Rotation;
            string rotation = rotationAngle.ToString();

            //Output the page rotation angle information
            Console.WriteLine("The rotation angle of the current page is: " + rotation);
        }
    }
}

C#: Get Page Size, Orientation and Rotation of PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.