Comparing documents

edit

PdfDocumentComparer compares what the pages of two documents show, rather than their bytes or their pixels:

var expected = PdfDocument.Open(expectedFile);
var actual = PdfDocument.Open(actualFile);
var comparison = PdfDocumentComparer.Compare(expected, actual);
var diff = comparison.ToString();

ToString writes the changes as a diff, a header per page that changed, then a line per change: - for an element of the first document, + for an element of the second one, ~ for an element that moved:

For instance, - text "Second line", followed by its bounding box, is a line of the first document that the second one does not have, + stroke 0 G [0 0 200 200] a path the second document strokes, and ~ fill 1 0 0 rg [50 50 90 90] -> [60 50 100 90] a path that moved.

What is compared

The pages are compared in order, the page n of a document with the page n of the other one; the pages left over are Added or Removed. Changes combines the kinds of changes of a page:

PdfPageChangeKindsChange
Size, RotationThe size of the crop box, the rotation
TextLines of text added or removed: a diff of the lines
TextGeometryThe same lines, at other places
Images, ImagePlacementsImages added, removed or changed (size in pixels, data); the same images moved
PathsPaths added, removed, moved, or painted with other colors
ShadingsShadings added, removed, changed or moved
  • Two documents written differently (object numbers, compression, object streams, linearization) that show the same elements are equivalent (AreEquivalent).
  • Positions closer than Tolerance (half a point by default) are the same. MaxDetails limits the lines given for each kind of change.
  • The colors of the paths are compared by the operators that set them (1 0 0 rg), and the elements by their bounding boxes: a path whose shape changes inside the same box is not reported. Render the pages to compare their pixels.

The elements of a page

PdfPageElements.Create gives what the comparer compares: the lines of text, the images (size, digest of their data, position), the paths painted (bounding box, fill or stroke, colors) and the shadings, in default user space, forms included. The snapshots of Meziantou.Framework.SnapshotTesting.Pdf write them with the Content metadata, so that the diff of a snapshot shows the same changes.