Page Orientation vs. Rotation Detection
Determining whether a PDF page is portrait or landscape, and whether its content is rotated, can be done two ways in the SDK. The right choice depends on whether the file carries reliable metadata.
PdfDocument.Pages[i].PageRotation reads rotation metadata embedded in the PDF. OcrInput.DetectPageOrientation() detects orientation by analyzing the visual layout with OCR.
Orientation vs. Rotation
These are two different measurements:
- Page orientation: the page layout, either Portrait (height greater than width) or Landscape (width greater than height).
- Rotation angle: how the content on the page is rotated relative to the natural reading direction, such as 90 degrees or 180 degrees.
Option 1: Read Metadata with PageRotation
IronPDF returns the PageRotation value by reading the rotation metadata embedded in the PDF file. It reports how the content is programmatically rotated, which stays reliable and consistent when the PDF is well-formed.
var pdf = PdfDocument.FromFile("sample.pdf");
for (int i = 0; i < pdf.PageCount; i++)
{
var page = pdf.Pages[i];
var orientation = page.Width > page.Height ? "Landscape" : "Portrait";
var rotation = page.PageRotation;
Console.WriteLine($"Page {i + 1}: {orientation}, Rotation: {rotation} degrees");
}Dim pdf = PdfDocument.FromFile("sample.pdf")
For i As Integer = 0 To pdf.PageCount - 1
Dim page = pdf.Pages(i)
Dim orientation = If(page.Width > page.Height, "Landscape", "Portrait")
Dim rotation = page.PageRotation
Console.WriteLine($"Page {i + 1}: {orientation}, Rotation: {rotation} degrees")
Next iDim pdf = PdfDocument.FromFile("sample.pdf")
For i As Integer = 0 To pdf.PageCount - 1
Dim page = pdf.Pages(i)
Dim orientation = If(page.Width > page.Height, "Landscape", "Portrait")
Dim rotation = page.PageRotation
Console.WriteLine($"Page {i + 1}: {orientation}, Rotation: {rotation} degrees")
Next iThis gives you orientation from the width/height comparison and the rotation angle from the PDF metadata in one pass.
Best for: structured PDFs with consistent metadata.
Option 2: Detect Visually with DetectPageOrientation()
IronOCR detects the orientation of a PDF page by analyzing the visual layout of each page with OCR and inferring the likely reading direction. It does not rely on metadata at all.
using var input = new OcrInput();
input.LoadPdf("sample.pdf");
var results = input.DetectPageOrientation();
foreach (var result in results)
{
Console.WriteLine($"Page {result.PageNumber + 1}: Rotation: {result.RotationAngle} degrees");
}using var input = new OcrInput();
input.LoadPdf("sample.pdf");
var results = input.DetectPageOrientation();
foreach (var result in results)
{
Console.WriteLine($"Page {result.PageNumber + 1}: Rotation: {result.RotationAngle} degrees");
}Using input As New OcrInput()
input.LoadPdf("sample.pdf")
Dim results = input.DetectPageOrientation()
For Each result In results
Console.WriteLine($"Page {result.PageNumber + 1}: Rotation: {result.RotationAngle} degrees")
Next
End UsingUsing input As New OcrInput()
input.LoadPdf("sample.pdf")
Dim results = input.DetectPageOrientation()
For Each result In results
Console.WriteLine($"Page {result.PageNumber + 1}: Rotation: {result.RotationAngle} degrees")
Next
End UsingReach for this with scanned documents, image-based PDFs, and files that lack proper metadata.
Accuracy depends heavily on text density and clarity. Sparse or misaligned text can produce incorrect detection. To work around this, pass a Rectangle and run OCR on an area with denser text so the orientation result stays correct.
Recommendation
Pick the method that matches your source material:
- Use
PdfDocument.PageRotationfor digital PDFs that carry correct metadata. - Use
OcrInput.DetectPageOrientation()for scanned or image-based PDFs where metadata is missing or unreliable.
For critical workflows, run both methods and apply custom logic to resolve any discrepancies. If you see inconsistent rotation results, check the specific page layout and validate with both methods together.
Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.