Recursive AI

Read PDF review annotations in C#

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

Read the highlight comments in marked.pdf, a OneDiff review PDF included in the zip. page.Annotations isn’t limited to OneDiff output: it also read the highlight comment in a PDF marked with a separate tool.

Terminal
dotnet add package RecursiveAI.OneDiff.Pdf

This package includes OnePDF as a dependency, so the snippet uses using OnePdf;.

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf;
using var pdf = PdfDocument.Open("marked.pdf");
foreach (var page in pdf.Pages)
foreach (var mark in page.Annotations)
    if (mark.Subtype == "Highlight")
        Console.WriteLine(mark.Contents);

Unzip the example, then dotnet run.

Prints:

Text
+ Replaced
30 days

The full program

C#
using OnePdf;
 
string path = args.Length == 0 ? "marked.pdf" : args[0];
using var pdf = PdfDocument.Open(path);
int count = 0;
for (int i = 0; i < pdf.PageCount; i++)
{
    foreach (var mark in pdf.Pages[i].Annotations)
    {
        if (mark.Subtype != "Highlight") continue;
        Console.WriteLine($"Page {i + 1}:");
        Console.WriteLine(mark.Contents);
        count++;
    }
}
Console.WriteLine($"Highlights: {count}");
Text
Page 1:
+ Replaced
30 days
Highlights: 1

The included marked.pdf is a generated one-page sample. Its visible text is “Pay in 60 days.” The comparison against “Pay in 30 days.” highlighted the replacement and put the old text in the highlight comment. To read a different PDF, run dotnet run -- review.pdf. A page without highlight annotations contributes nothing to the count; if there are none anywhere, the program prints Highlights: 0.

Variations

Count the marked areas. Inside the highlight loop, print the number of quadrilaterals: each is a four-cornered area represented by eight coordinates.

C#
Console.WriteLine(mark.QuadPoints.Count / 8);

Read other text markup too. Replace the full program’s subtype check with this one if you also want strikeouts, underlines, and wavy underlines:

C#
if (mark.Subtype is not ("Highlight" or "StrikeOut"
    or "Underline" or "Squiggly")) continue;

The short answer

Open the marked file with PdfDocument.Open, loop over page.Annotations, and select the Highlight subtype. Contents holds the highlight comment, if present. You only need the marked PDF for this step, not both files from the original comparison.

How do you read PDF annotations without extracting the whole page?

Annotations are available separately from page text. Start with the page index and the annotation subtype, then read the highlight comment, if present. If you want to place the mark on a preview, keep the page index and QuadPoints as well. One highlight can have more than one marked area, for example when the text crosses a line break. The variation counts areas on each highlight, not how many highlights the page has.

The highlight comment is not the highlighted text. In this sample, Contents starts with + Replaced and then gives 30 days; the annotated page is the newer version, which says 60 days. To recover what the mark covers rather than what the highlight comment says, you need to match the highlight geometry against extracted page text. As the OneDiff PDF documentation describes, OneDiff can round part of a changed word out to a whole word when drawing the mark, so geometry and the exact text change are not interchangeable.

Why might a PDF show highlights but have no highlight annotations?

In OneDiff, original-page markup adds annotations to the newer PDF. A rendered redline instead draws its changes as part of a newly laid-out report. Both can show colored changes in a viewer, but the loop above reads the annotation objects, not the colors drawn on a page. If you control how the review PDF is created and need to read its notes later, use original-page markup rather than the rendered report.

All onediff recipes