---
title: "Parse a PDF file in C#"
url: https://recursiveai.org/onepdf/cookbook/parse-pdf-structure/
description: "Parse a PDF file in C# with OnePDF. Read the page tree, page count, page boxes, rotation, and document metadata without rendering the pages."
version: "1.25.0"
---

# Parse a PDF file in C#

**OnePDF** is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf) · [GitHub](https://github.com/recursive-ai-oss/onepdf-csharp) · [Docs](https://recursiveai.org/onepdf/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/cookbook/parse-pdf-structure/parse-pdf-structure.zip)

Read the page sizes and title without rendering the PDF. The example uses `field-notes.pdf` from the download, a blank two-page file titled “Field Notes.” This reads the PDF’s structure; to get the words on a page, see [Extract text from a PDF](https://recursiveai.org/onepdf/cookbook/extract-text/).

```shell
dotnet add package RecursiveAI.OnePdf
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OnePdf;

using var pdf =
    PdfDocument.Open("field-notes.pdf");
Console.WriteLine(pdf.Info.Title);
foreach (var page in pdf.Pages)
{
    var box = page.CropBox;
    Console.WriteLine(
        $"{box.Width} × {box.Height}");
}
```

Unzip [the example](https://recursiveai.org/onepdf/cookbook/parse-pdf-structure/parse-pdf-structure.zip), then `dotnet run`.

Prints the title and each page’s crop width and height before rotation:

```text
Field Notes
612 × 792
540 × 720
```

## The full program

The media box is the full sheet. The crop box is the visible area. The sample’s pages inherit a 612 × 792 media box from the page tree. Page 2 sets a crop rectangle of `[36 36 576 756]` and a clockwise rotation of 90°. The full program compares the stored geometry with the size a viewer displays. It also reads the author (“River Survey Team”) and checks for XMP, a separate metadata packet that this sample omits.

```csharp
using OnePdf;

string path = args.Length == 0 ? "field-notes.pdf" : args[0];
if (!PdfDocument.TryOpen(path, out var pdf, out var error))
{
    Console.Error.WriteLine(error.Message);
    return 1;
}

using (pdf)
{
    Console.WriteLine($"PDF {pdf.Version}, {pdf.PageCount} pages");
    Console.WriteLine($"Info title: {pdf.Info.Title ?? "(none)"}");
    Console.WriteLine($"Info author: {pdf.Info.Author ?? "(none)"}");
    Console.WriteLine($"XMP title: {pdf.Xmp?.Title ?? "(none)"}");

    foreach (PdfPage page in pdf.Pages)
    {
        var display = page.GetDisplaySize();
        Console.WriteLine($"Page {page.Index + 1}: {page.Rotate}° clockwise");
        Console.WriteLine($"  Media: {page.MediaBox.Width} × {page.MediaBox.Height}");
        Console.WriteLine($"  Crop: {page.CropBox.Width} × {page.CropBox.Height}");
        Console.WriteLine($"  Display: {display.Width} × {display.Height} pt");
    }
}
return 0;
```

```text
PDF 1.7, 2 pages
Info title: Field Notes
Info author: River Survey Team
XMP title: (none)
Page 1: 0° clockwise
  Media: 612 × 792
  Crop: 612 × 792
  Display: 612 × 792 pt
Page 2: 90° clockwise
  Media: 612 × 792
  Crop: 540 × 720
  Display: 720 × 540 pt
```

If the PDF data can’t be parsed, `TryOpen` returns the error; this program prints it and exits with code 1. A missing file still throws an I/O exception.

## Variations

**Get the first page’s displayed size in inches.** Replace the `Console.WriteLine` and loop in the snippet at the top:

```csharp
if (pdf.PageCount > 0)
{
    var size = pdf.Pages[0].GetDisplaySize();
    Console.WriteLine(FormattableString.Invariant(
        $"{size.WidthInches:0.#} × {size.HeightInches:0.#} in"));
}
```

```text
8.5 × 11 in
```

## Related recipes

- [Extract text from a PDF](https://recursiveai.org/onepdf/cookbook/extract-text/)
- [Export PDF extraction results as JSON](https://recursiveai.org/onepdf/cookbook/export-pdf-extraction-as-json/)
- [Repair a damaged PDF](https://recursiveai.org/onepdf/cookbook/repair-damaged-pdf/)

## The short answer

To parse a PDF file in C#, open it with `PdfDocument.Open`. OnePDF reads the page tree and exposes its pages in document order through `pdf.Pages`. Read the boxes on each page and use `pdf.Info` or `pdf.Xmp` for document metadata. Dispose the document when you finish; it holds the source open and reads from it as needed. You don’t need to render pages or extract text to inspect their geometry.

## How do you get the PDF page count in C#?

Read `pdf.PageCount`, as in the full program. Page indexes start at zero: `pdf.Pages[0]` is page 1. Use `page.Index + 1` to print a human-readable position when you enumerate the pages. A PDF can also define page labels, such as Roman numerals for its front matter. `page.Label` reads that label when the file supplies one; it isn’t an array index.

The [page geometry docs](https://recursiveai.org/onepdf/docs/page-geometry/) call these boxes “the *effective* rectangles.” A page can inherit a media box, crop box, or rotation from an ancestor in the page tree. The media box describes the full sheet; the crop box describes the region shown or printed. If there is no crop box, OnePDF uses the media box. Bleed, trim, and art boxes default to the crop box. Use `GetDisplaySize()` for dimensions that account for rotation and the PDF’s user-unit scale. That’s why page 2 in the example measures 540 × 720 before rotation but displays as 720 × 540 points.

## Can you extract metadata from a PDF?

Yes. `pdf.Info` reads the document’s Info dictionary, including fields such as `Title` and `Author`. `pdf.Xmp` reads the separate XMP packet when one exists. They’re independent fields: the sample has an Info title but no XMP title. If you’re inventorying PDFs, record which source a value came from and decide what to do when the two sources disagree rather than assuming one always contains the other.

## How can I extract data from a PDF using C#?

Decide first whether you need facts about the file or words drawn on the page. Use the page and document properties above to build an inventory, for example to find files containing rotated or unusually sized pages. A title drawn on a cover page is page content, not necessarily the document’s `Info.Title`. For the words a reader sees, use [text extraction](https://recursiveai.org/onepdf/cookbook/extract-text/), which works with page content and reading order instead of the page tree.
