Recursive AI

Save a PDF incrementally in C#

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

An incremental save appends changes to a byte-for-byte copy, keeping the original bytes intact for signature checks.

Terminal
dotnet add package RecursiveAI.OnePdf

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

C#
using OnePdf;
 
using var doc = PdfDocument.Open("sample.pdf",
    new PdfOpenOptions { AllowEditing = true });
doc.SetInfo("Title", "Approved");
using var output = File.Create("updated.pdf");
doc.SaveIncremental(output);
Console.WriteLine("Wrote updated.pdf");

Unzip the example, then dotnet run.

Text
Wrote updated.pdf

updated.pdf has the new title. Keep the output path different from the input path: File.Create truncates the file it opens.

The full program

The one-page sample.pdf in the zip has the title “Draft.” The download reopens the output and compares its title and revision count with the original.

C#
using OnePdf;
 
var edit = new PdfOpenOptions { AllowEditing = true };
using var doc = PdfDocument.Open("sample.pdf", edit);
Console.WriteLine($"Original title: {doc.Info.Title}");
doc.SetInfo("Title", "Approved");
using (var output = File.Create("updated.pdf"))
{
    doc.SaveIncremental(output);
}
 
using var saved = PdfDocument.Open("updated.pdf");
Console.WriteLine($"Saved title: {saved.Info.Title}");
Console.WriteLine($"Revisions: {doc.Revisions.Count} -> {saved.Revisions.Count}");
Text
Original title: Draft
Saved title: Approved
Revisions: 1 -> 2

If the PDF had to be rebuilt by scanning, SaveIncremental throws PdfFormatException: there’s no original cross-reference section to link to. Use a full Save for repaired files.

Variations

Write to memory. Replace the last three lines of the snippet at the top with this to get a complete PDF as a byte array:

C#
using var output = new MemoryStream();
doc.SaveIncremental(output);
byte[] pdf = output.ToArray();

Use a full save instead. Replace those same lines with this when you don’t want to carry over the original file as an earlier revision:

C#
doc.Save("rewritten.pdf");

The short answer

To save an incremental PDF update in C#, open an existing file with AllowEditing = true, edit it, then call doc.SaveIncremental(output) with a new writable stream. The result is a whole PDF, not a patch you have to apply. Its first bytes are identical to the input, and the appended revision is what makes a reader see the new title in this example.

What is an incremental update in a PDF?

A PDF reader uses cross-reference data to find the objects that make up the document. Instead of writing every object again, an incremental save appends the changed or added objects, cross-reference entries for the update, and a trailer that points back to the previous cross-reference section via /Prev. The viewer follows that chain to get unchanged objects from earlier revisions. In this example, the original title is still in the old revision, even though a viewer opening the latest one sees “Approved.” The writing reference describes how OnePDF writes the update.

Since the output includes the original, it still has to copy the original bytes to the destination. The benefit is keeping those bytes untouched and writing the edit on top rather than rebuilding the PDF. This is not a way to shrink the file or remove data from it.

How do I save changes to a PDF more than once?

Every call saves the changes since the document was opened, not the changes since the last call. If you want the next edit to produce a third revision after the two in the example, close the output, open updated.pdf for editing, make the next change, and save to another new stream. If you instead keep the original doc open, each save still starts with sample.pdf; it does not extend the first saved copy.

When should I use a full save instead of an incremental update?

Use a full save when writing a repaired PDF, removing or changing encryption, or asking for a PDF/A or PDF/UA conformance save. OnePDF also refuses an incremental save when it finds lost page-tree entries or when you’ve applied redaction, because the appended revision can’t remove bytes from the source. If the file is signed, keeping the original bytes is not the same as being allowed to edit it: a certification signature may restrict the changes a reader accepts. See signature verification before relying on an updated file’s signature status.

All onepdf recipes