Check two PDFs for equivalence in C#
Find out whether a PDF written by any tool still holds the same document as the original.
Short answer
Section titled “Short answer”PdfEquivalence.FindDifference walks both object graphs from the trailer and compares every
dictionary, array and reference, and every stream after decoding. It returns null when the two
are equivalent, or a description of the first difference.
byte[] original = File.ReadAllBytes("report.pdf");foreach (string candidate in new[] { "report-optimized.pdf", "report-edited.pdf" }){ string? difference = PdfEquivalence.FindDifference(original, File.ReadAllBytes(candidate)); Console.WriteLine($"{candidate}: {difference ?? "equivalent"}");}dotnet add package PdfNetStackIn the sample, the optimized copy is equivalent and the copy with a page removed is not:
trailer/Root/Pages/Kids: array length 3 vs 2.
This is the check the optimizer runs on its own output. Encoding keys such as /Length, /Filter
and /DecodeParms may differ. JPEG images must match coefficient by coefficient; pass
allowJpegRequantization: true to accept requantized JPEGs. From 1.1, an overload that also takes
minImageSsim accepts images that another tool downsampled or converted to JPEG, when they match
the source resampled to their size; soft masks, masks and stencil images must still match exactly.
The method and its limits are on the
fidelity page.
Related
Section titled “Related”- Compress PDF
- Command line:
pdfnetstack verify original.pdf other.pdf - API: PdfEquivalence