When working with PDF files, it is crucial to understand information such as page size, orientation and rotation angle for PDF display, printing and layout design. This article will introduce how to use the free .NET library through C# to read these properties of the PDF page.
- C# read PDF page size (width, height)
- C# judgment PDF page direction
- C# Detect PDF page rotation angle
free libraryFree for .NET Provides an interface to obtain PDF page information, we can from the official websiteDownload Product PackagesAdd the reference manually afterward, or install it directly via NuGet.
PM> Install-Package
Enter the document as shown:
C# read PDF page size (width, height)
Provided free of charge respond in singing property to get the width and height of the specified PDF page.
The default unit of the obtained value is point, if you want to convert it to centimeters, millimeters and other common units, you can pass thePdfUnitConvertor classConvertUnits(float value, PdfGraphicsUnit from, PdfGraphicsUnit to) method to perform the conversion.
The sample code is as follows:
using System; using ; using ; using ; namespace GetPDFPageSize { class Program { static void Main(string[] args) { //Loading PDF files PdfDocument pdf = new PdfDocument(); ("Example.pdf"); //Get the first page PdfPageBase page = [0]; //Get the width and height of the page (default unit is point) float pointWidth = ; float pointHeight = ; //Create PdfUnitConvertor object used to convert units PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); //Convert units from pounds (points) to centimeters (cm) float centimeterWidth = (pointWidth, , ); float centimeterHeight = (pointHeight, , ); //Converting units from pounds (points) to millimeters float millimeterWidth = (pointWidth, , ); float millimeterHeight = (pointHeight, , ); //Output PDF page width and height information ("The PDF page size is (in pounds): Width" + pointWidth + "pt, height" + pointHeight + "pt"); ("The PDF page size is (in centimeters): Width" + centimeterWidth + "cm, height" + centimeterHeight + "cm"); ("The PDF page size is (in millimeters): Width" + millimeterWidth + "mm, height" + millimeterHeight + "mm"); } } }
Output results:
C# judgment PDF page direction
The orientation of a page is usually expressed in landscape or portrait. To determine the orientation of a specified PDF page:
- Get the width and height of the page first
- Compare the two values again. (If the width is greater than the height, the page orientation is landscape, and vice versa.)
The sample code is as follows:
using ; using System; namespace GetPDFPageOrientation { class Program { static void Main(string[] args) { //Load PDF document PdfDocument pdf = new PdfDocument(); ("Example.pdf"); //Get the first page PdfPageBase page = [0]; //Get page width and height float width = ; float height = ; //Determine page orientation by comparing page width and height if (width > height) { ("The current page orientation is landscape."); } else { ("The current page orientation is portrait."); } } } }
Output results:
C# Detect PDF page rotation angle
pass (a bill or inspection etc) property gets the rotation angle of the specified PDF page. If it is 0, it means the page keeps the original orientation.
The sample code is as follows:
using ; using System; namespace GetPDFPageOrientation { class Program { static void Main(string[] args) { //Load PDF document PdfDocument pdf = new PdfDocument(); ("Example.pdf"); //Get the first page PdfPageBase page = [0]; //Get the rotation angle of the page and output the result PdfPageRotateAngle rotationAngle = ; string rotation = (); ("The current page rotation is." + rotation); } } }
Output results:
- If you have any questions, you can go toforum (for discussion)Exchange.