Round-trip validation

edit

PdfRoundTripValidator writes a document, reads the output back, writes it again, and compares the two outputs:

var result = PdfRoundTripValidator.Validate(file);
Assert.True(result.Succeeded, result.Failure?.ToString()); // Failure.Kind tells which check failed

Use it in the tests of an application that changes documents: a change that the writer cannot write back the same way (a damaged object, a value of the wrong type) shows up as a failure rather than as a file that changes each time it is saved.

What is checked

The checks run in order, and Failure is the first one that fails:

PdfRoundTripFailureKindMeaning
ReadBackFailedAn output cannot be read, for instance an encrypted output read without its password
ReadBackRepairedAn output is damaged: the reader repairs it
PageCountChangedAn output does not have the pages of the document it was written from
PageChangedA page changed: its media box or crop box, rotation, number of annotations or decoded content (PageIndex)
ObjectGraphChangedThe two outputs do not have the same objects
OutputsDifferThe two outputs have the same objects but different bytes
  • The boxes of the first output may differ from those of the document by the rounding of the numbers: the writer writes at most six decimals.
  • The bytes of an encrypted document are not compared (ComparesBytes is false): each save encrypts with new random initialization vectors. Pass the password in the read options to read the outputs.
  • Validate takes the options of both saves, so that a linearized file or a file without object streams can be checked too.

Summaries of documents

The result has a PdfDocumentSummary of the document and of each output: the pages (boxes, rotation, annotations and a digest of the content), the number of objects by kind and by /Type, and a Digest of the object graph. The digest does not depend on the numbers of the objects or on the way they are stored (object streams, cross-reference streams, linearization): two documents with the same digest have the same objects. PdfDocumentSummary.Create summarizes any document.