FPdfDiff supplies the page alignment, UTF-16 text-change and connected visual-region routines behind CompareWith
AlignPdfDiffPages uses a resource-limited longest common subsequence to pair matching text, geometry and visual fingerprints; it treats remaining exact matches as moves, then pairs unmatched gaps as modifications
Repeated exact pages receive deterministic pairings; the legacy overload retains exact matching and gap pairing
The overload accepting TPdfDiffAlignmentOptions can match pages that move and change together when MatchSimilarPages is True; Default leaves it False
The heuristic compares distinct packed UTF-16 trigrams after collapsing control and space runs to a single ASCII space, preserving case and punctuation; its Dice score is an integer from 0 to 1000, where 1000 indicates the same trigram set rather than guaranteed identical content
A candidate requires equal rotation and dimensions within 0.001, enough text and distinct trigrams, and a mutual best score separated from both competing candidates; ties are never confirmed, even with a zero margin
MinimumSimilarity defaults to 800 (valid 1..1000), MinimumSimilarityMargin to 100 (0..1000), MinimumTextCharacters to 24 (3..16777216) and MinimumDistinctTrigrams to 16 (1..16777216)
MaxSimilarityCharacters defaults to 16777216 (valid 1..16777216) across unmatched input text; MaxSimilarityComparisons defaults to 16777216 (1..268435456) and counts candidate visits and trigram merge comparisons; exceeding either budget raises an exception rather than returning a partial alignment
Exact stable anchors are retained, then the longest ordered sequence of confident candidates is selected; candidates outside that sequence become dpkMovedModified, whilst ordered candidates are dpkModified
Similarity is 1000 for exact pairs, the heuristic score for confirmed similar pairs, or zero for positional gap pairs and unmatched pages; SimilarityAmbiguous flags an unresolved left candidate whose best score met the threshold, including a later positional gap pair
Unconfirmed candidates still follow the legacy positional gap fallback and are never presented as confirmed movement; blank, image-only, short, low-information or differently sized pages need other identity evidence and fall outside this text-similarity profile
Similarity scores express evidence, not proof of identity; actual text and pixel comparison still runs on each paired page, and no source document is changed
BuildPdfDiffTextChanges supplies zero-based UTF-16 offsets and lengths; character mode never separates a surrogate pair, and word mode groups whitespace apart from other characters
BuildPdfDiffPixelRegions combines connected changed tiles into pixel rectangles with exclusive right and bottom edges; an unchanged pixel inside a tile may still lie within the returned region
Alignment permits 4,096 pages on either side and 16,777,216 cells; text processing permits 65,536 tokens and 16,777,216 UTF-16 units, and explicitly reports truncation when a chosen detail budget runs out
TPdfDiffPageSignature fingerprints one page for alignment: Text carries the bounded extracted page text, VisualHash hashes a 64x64 white-background render, and Width, Height and Rotation record the page geometry; two signatures count as equal when text, hash and rotation match and dimensions agree within 0.001
TPdfDiffPageSignatures is the dynamic array of page signatures, one per page, that AlignPdfDiffPages receives for each side
TPdfDiffPagePair is one alignment outcome: LeftPageIndex and RightPageIndex are zero-based page indexes with -1 on the side that has no counterpart, and Kind classifies the pair; TPdfDiffPagePairs is the dynamic array of these outcomes
TPdfDiffPageKind values classify each pair: dpkMatched pairs identical pages inside the stable sequence, dpkModified pairs pages whose signatures differ, dpkMoved pairs an exact duplicate outside that sequence, dpkInserted marks a right-only page, dpkDeleted marks a left-only page, and dpkMovedModified identifies a confident similar page outside the stable sequence
TPdfDiffTextMode selects the text comparison granularity: dtmNone skips text changes, dtmCharacters diffs UTF-16 code units with surrogate pairs kept together, and dtmWords diffs word runs with whitespace grouped separately
TPdfDiffTextChange describes one replaced span through LeftStart, LeftLength, RightStart and RightLength; TPdfDiffTextChanges is the dynamic array of those spans
TPdfDiffPixelRegion is one changed rectangle in mask pixels, held in Left, Top, Right and Bottom; TPdfDiffPixelRegions is the dynamic array returned by BuildPdfDiffPixelRegions
type
TPdfDiffPageKind= (dpkMatched, dpkModified, dpkMoved, dpkInserted, dpkDeleted,
dpkMovedModified);
TPdfDiffTextMode= (dtmNone, dtmCharacters, dtmWords);
TPdfDiffPageSignature= record
Text: WideString;
VisualHash: UInt64;
Width, Height: Single;
Rotation: Integer;
end;
TPdfDiffPageSignatures= array of TPdfDiffPageSignature;
TPdfDiffPagePair= record
LeftPageIndex, RightPageIndex: Integer;
Kind: TPdfDiffPageKind;
Similarity: Integer;
SimilarityAmbiguous: Boolean;
end;
TPdfDiffPagePairs= array of TPdfDiffPagePair;
TPdfDiffAlignmentOptions= record
MatchSimilarPages: Boolean;
MinimumSimilarity: Integer;
MinimumSimilarityMargin: Integer;
MinimumTextCharacters: Integer;
MinimumDistinctTrigrams: Integer;
MaxSimilarityCharacters: Integer;
MaxSimilarityComparisons: Integer;
class function Default: TPdfDiffAlignmentOptions; static;
end;
// Offsets and lengths count UTF-16 code units, starting at zero
TPdfDiffTextChange= record
LeftStart, LeftLength, RightStart, RightLength: Integer;
end;
TPdfDiffTextChanges= array of TPdfDiffTextChange;
TPdfDiffPixelRegion= record
Left, Top, Right, Bottom: Integer;
end;
TPdfDiffPixelRegions= array of TPdfDiffPixelRegion;
function AlignPdfDiffPages(const Left, Right: TPdfDiffPageSignatures;
MaxCells: Integer): TPdfDiffPagePairs; overload;
function AlignPdfDiffPages(const Left, Right: TPdfDiffPageSignatures;
MaxCells: Integer; const Options: TPdfDiffAlignmentOptions): TPdfDiffPagePairs; overload;
function BuildPdfDiffTextChanges(const Left, Right: WideString;
Mode: TPdfDiffTextMode; MaxCells, MaxTokens: Integer;
out Truncated: Boolean): TPdfDiffTextChanges;
function BuildPdfDiffPixelRegions(const Mask: TBytes; Width, Height,
TileSize, MaxRegions: Integer; out Truncated: Boolean): TPdfDiffPixelRegions;