Recursive AI

Render a text comparison as a PDF redline in C#

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

Compare before.txt and after.txt from the zip to lay out a new PDF with changes marked.

Terminal
dotnet add package RecursiveAI.OneDiff.Pdf

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

C#
using OneDiff.Documents;
using OneDiff.Pdf;
 
var comparison = Comparison.Of(
    "before.txt", "after.txt");
comparison.SaveAs("redline.pdf",
    PdfRedline.Rendered);
Console.WriteLine("Saved redline.pdf");

The zip contains two one-line documents, each ending with a newline:

before.txtafter.txt
Please submit the draft on Monday.Please submit the draft on Friday.

The default inline style strikes through “Monday” and double-underlines “Friday.” On the redline page, select both words in your PDF viewer to copy the old and new text. The small example has a cover page before the comparison.

If the comparison is too large for the renderer to lay out, it throws InvalidOperationException; try the HTML redline instead.

Unzip the example, then dotnet run.

Writes redline.pdf and prints:

Text
Saved redline.pdf

Variations

Replace the SaveAs call with one of these, and update the filename in Console.WriteLine to match:

Put the old and new text in separate columns on landscape pages. Writes side.pdf.

C#
comparison.SaveAs("side.pdf",
    PdfRedline.RenderedSideBySide);

Use US Letter instead of A4. Writes letter.pdf.

C#
var letter = new RenderedPdfRedline
{
    Paper = PdfPaper.Letter
};
comparison.SaveAs("letter.pdf", letter);

The short answer

Compare the old and new files with Comparison.Of, then save the result with PdfRedline.Rendered from RecursiveAI.OneDiff.Pdf. The output is a new, paginated PDF with selectable text, not a copy of either input. Deletions and insertions appear together in the body, so the reader can see the old wording and its replacement in context. Use the side-by-side form when you want to read each version in its own column.

What is a PDF redline?

A redline, also called a blackline, is a review copy that shows the changes between two versions. In the default inline layout, deleted text stays in the flow but is struck through; additions are double-underlined. That lets a reviewer see the change without opening both source files. The cover identifies the inputs and summarizes the changes. The footer gives their names and hashes so the review copy identifies the versions used to make it.

How do you compare two PDFs and highlight differences?

Choose the output based on what you want to keep. A rendered redline uses the comparison to lay out new pages. That makes it useful for plain text, where there are no source pages to preserve. You can also render a comparison of digital PDFs, but the new result should not be treated as a copy of their page layouts, graphics, or form fields. Those items are not all present in the PDF comparison model.

If a reviewer needs the original page design, use markup on the revised PDF instead. That form adds marks on the existing pages using locations from the PDF text. It needs the revised PDF’s bytes; there’s no equivalent page to mark when your input is a text file.

How can I tell if a PDF is text searchable?

Try selecting a changed word and searching for it in the viewer. In the sample, search for “Friday” on the redline page. You may also find “Monday”: deleted wording is still in the PDF, with a strike-through. Text copied from an inline redline can put old and new text next to each other, so don’t use it as the clean revised text. Share the revised source separately if readers need a clean copy.

Text being searchable does not guarantee the same characters are visible. The renderer ships with Liberation Sans. For scripts it doesn’t cover, reference the OneRender font package for that script. Check how those words render as well as whether they can be selected.

All onediff recipes