---
title: "Export PDF text and geometry as JSON in C#"
url: https://recursiveai.org/onepdf/cookbook/export-pdf-extraction-as-json/
description: "Export PDF text and geometry as JSON in C#. Runnable .NET example with sample input, actual output and documented options."
version: "1.22.0"
---

# Export PDF text and geometry as JSON 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/export-pdf-extraction-as-json/export-pdf-extraction-as-json.zip)

Export words and glyph positions as structured JSON.

```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 OnePdf.Text;

// input.pdf is in the example zip.
using var doc =
    PdfDocument.Open("input.pdf");
var pages = doc.Pages
    .Select(page => page.ExtractText())
    .ToArray();
using var output =
    File.Create("extraction.json");
PdfExtractionJson.Write(output, pages);
```

Writes `extraction.json`; prints nothing. `input.pdf` is in the zip: one 300 × 200 point page with “Hello JSON” at (30, 150), in 12-point Helvetica.

Unzip [the example](https://recursiveai.org/onepdf/cookbook/export-pdf-extraction-as-json/export-pdf-extraction-as-json.zip), then `dotnet run`.

## The full program

The full program creates that input PDF before extracting it, then reads the JSON with `System.Text.Json`. It checks the format and version before inspecting the first word. A missing input path throws `FileNotFoundException` before an output is created.

```csharp
using OnePdf;
using OnePdf.Text;
using System.Text.Json;

using (var source = PdfDocument.CreateEmpty())
{
    var page = source.InsertBlankPage(0, 300, 200);
    using var content = page.BeginContent();
    content.SetFont(source.AddStandardFont("Helvetica"), 12);
    content.DrawText("Hello JSON", 30, 150);
    content.Commit();
    source.Save("input.pdf");
}
using var doc = PdfDocument.Open("input.pdf");
var pages = doc.Pages.Select(page => page.ExtractText()).ToArray();
using (var output = File.Create("extraction.json"))
    PdfExtractionJson.Write(output, pages,
        new PdfExtractionJsonOptions { Indented = true });
using var json = JsonDocument.Parse(File.ReadAllText("extraction.json"));
var root = json.RootElement;
if (root.GetProperty("format").GetString() != "onepdf.extraction" ||
    root.GetProperty("version").GetInt32() != 1)
    throw new InvalidDataException("Unknown extraction format");
var first = root.GetProperty("pages")[0];
var word = first.GetProperty("blocks")[0].GetProperty("lines")[0]
    .GetProperty("words")[0];
Console.WriteLine($"Format: {root.GetProperty("format").GetString()} v1");
Console.WriteLine($"Page: {first.GetProperty("width")} x {first.GetProperty("height")} pt");
Console.WriteLine($"Text: {first.GetProperty("text").GetString()}");
Console.WriteLine($"Word: {word.GetProperty("text").GetString()}");
Console.WriteLine($"Bounds: {JsonSerializer.Serialize(word.GetProperty("bounds"))}");
Console.WriteLine($"Glyphs: {first.GetProperty("glyphs").GetArrayLength()}");
```

```text
Format: onepdf.extraction v1
Page: 300 x 200 pt
Text: Hello JSON
Word: Hello
Bounds: [30,147.6,57.336,160.8]
Glyphs: 10
```

## Variations

**Omit the page glyph array.** Replace the `Write` call in the snippet at the top; it writes JSON without a `glyphs` member on each page and prints nothing:

```csharp
// Replace the Write call in the snippet at the top.
PdfExtractionJson.Write(output, pages,
    new PdfExtractionJsonOptions { IncludeGlyphs = false });
```

## Related recipes

- [Extract PDF text](https://recursiveai.org/onepdf/cookbook/extract-text/)
- [Get word positions](https://recursiveai.org/onepdf/cookbook/word-positions/)

---

## The short answer

Extract each page with `page.ExtractText()`, then pass the results to `PdfExtractionJson.Write` or `ToJson`. The output contains page text, blocks, lines, words and optional per-glyph details. It is a defined interchange format, `onepdf.extraction` version 1, rather than an arbitrary serialization of the library’s object model.

Use this when downstream code needs both text and location: for example, to associate a word with a page region or compare extraction results across implementations. The full program shows a JSON reader checking the format identifier and version, then reading the first word’s bounding box. The [extraction JSON specification](https://recursiveai.org/onepdf/docs/extraction-json/) defines each member.

## How can I convert a PDF to text in C#?

Call `ExtractText()` on the pages in document order. The JSON page’s `text` member is the reading-order plain text: blocks are separated by two newlines, lines by one newline, and table cells by tabs. If you only need that string, the JSON container is optional. If you need positions, keep the structured blocks and words rather than trying to infer coordinates from spaces in a string.

The example writes a simple page with two words, then reads it back. Its first word is “Hello,” and the reported bounds are `[30,147.6,57.336,160.8]`. A glyph’s box covers its text geometry, so it is not simply the original text baseline and font size copied into four fields. Page dimensions and coordinates are already in upright page space: points, bottom-left origin, y increasing upward, with page rotation and the page box already applied.

Every box is ordered left, bottom, right, top. Keep that convention when translating coordinates into an interface whose origin is at the top left. Do not apply the PDF page’s rotation a second time to exported bounds. The JSON gives you the viewer-oriented geometry, unlike the unrotated default user space used by the low-level content builder that created this example.

## Is there a free C# PDF-to-JSON export example?

Yes. The project download uses the released OnePDF NuGet package under MIT or Apache-2.0 and runs without an online conversion service. It creates its own input, so the PDF and the corresponding JSON can be reproduced together. The output is text extraction data, not a reconstruction of every PDF object, picture or form field into JSON.

Check both `format` and `version` in a consumer. Additive members keep the version; renaming, removal or a change in meaning raises it. Tolerate members you do not need rather than assuming the current set can never grow. The writer emits UTF-8 without a byte-order mark and rounds coordinates to three decimals using ties to even. Member order is defined, and the same extraction input produces deterministic JSON. Indentation changes whitespace, not meaning.

Some fields deliberately have missing or null values. Glyph `font` and `color` can be null, unknown `fontId` is -1, and glyph text can be empty after an `ActualText` replacement. With `IncludeGlyphs = false`, the page’s glyph array is omitted rather than written as null. Words still carry glyph indices, but without that array a consumer cannot dereference them. Keep glyph output enabled when those references matter.

The glyph list is the extraction result, not every drawing operation in the original file. Clipped, off-page and duplicate glyphs have already been removed unless extraction options kept them. Invisible text can still be represented with `visible = false`. Preserve those distinctions if you use the export for search, highlighting or another transformation.
