# Page Orientation and Rotation in PDFs
Working with PDFs often means knowing two things about a page: its orientation (portrait or landscape) and whether the content is rotated. There are two ways to find out, and they answer slightly different questions.
- `PdfDocument.Pages[i].PageRotation` reads rotation from the PDF metadata.
- `OcrInput.DetectPageOrientation()` infers orientation visually with OCR.
## Orientation vs. Rotation
These are not the same property:
- **Page orientation:** the page layout. **Portrait** means height is greater than width; **Landscape** means width is greater than height.
- **Rotation angle:** how the content sits relative to the natural reading direction, for example `90`, `180`, and so on.
## Option 1: Read Rotation from Metadata
`PageRotation` returns the rotation value embedded in the PDF file, so it tells you how the content is programmatically rotated. For well-formed PDFs this is reliable and consistent.
```csharp
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");
}
```
You get both facts in one pass: orientation from the width-to-height comparison, and the rotation angle straight from the metadata.
**Best for:** structured PDFs with consistent, valid metadata.
## Option 2: Detect Orientation with OCR
When the metadata is missing or untrustworthy, analyze the page visually instead. `DetectPageOrientation()` examines the layout of each page and infers the likely reading direction, with no reliance on metadata.
```csharp
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");
}
```
This is the right choice for scanned documents, image-based PDFs, and any file lacking proper metadata.
Accuracy here tracks text density and clarity. Sparse or misaligned text can produce a wrong result, so restrict OCR to a `Rectangle` over a denser region of text to steady the detection.
## Recommendation
- **Digital PDFs:** prefer `PdfDocument.PageRotation` when the metadata is correct.
- **Scanned or image-based PDFs:** use `OcrInput.DetectPageOrientation()` where metadata is missing or unreliable.
For critical workflows, run both methods and apply your own logic to reconcile any discrepancy. If you are seeing inconsistent rotation results, check the specific page layout and validate one method against the other.
Working with PDFs often means knowing two things about a page: its orientation (portrait or landscape) and whether the content is rotated. There are two ways to find out, and they answer slightly different questions.
PdfDocument.Pages[i].PageRotation reads rotation from the PDF metadata.
OcrInput.DetectPageOrientation() infers orientation visually with OCR.
Orientation vs. Rotation
These are not the same property:
Page orientation: the page layout. Portrait means height is greater than width; Landscape means width is greater than height.
Rotation angle: how the content sits relative to the natural reading direction, for example 90, 180, and so on.
Option 1: Read Rotation from Metadata
PageRotation returns the rotation value embedded in the PDF file, so it tells you how the content is programmatically rotated. For well-formed PDFs this is reliable and consistent.
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");}
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");
}
C#
You get both facts in one pass: orientation from the width-to-height comparison, and the rotation angle straight from the metadata.
Best for: structured PDFs with consistent, valid metadata.
Option 2: Detect Orientation with OCR
When the metadata is missing or untrustworthy, analyze the page visually instead. DetectPageOrientation() examines the layout of each page and infers the likely reading direction, with no reliance on metadata.
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");
}
C#
This is the right choice for scanned documents, image-based PDFs, and any file lacking proper metadata.
Accuracy here tracks text density and clarity. Sparse or misaligned text can produce a wrong result, so restrict OCR to a Rectangle over a denser region of text to steady the detection.
Recommendation
Digital PDFs: prefer PdfDocument.PageRotation when the metadata is correct.
Scanned or image-based PDFs: use OcrInput.DetectPageOrientation() where metadata is missing or unreliable.
For critical workflows, run both methods and apply your own logic to reconcile any discrepancy. If you are seeing inconsistent rotation results, check the specific page layout and validate one method against the other.
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.