FPdfDiff implements page alignment, UTF-16 text changes and connected visual regions used by CompareWith
AlignPdfDiffPages pairs exact text, geometry and visual fingerprints through a bounded longest common subsequence, then identifies remaining exact matches as moves and pairs unmatched gaps as modifications
Duplicate exact pages are paired deterministically; 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/space runs to one ASCII space, preserving case and punctuation; its Dice score is an integer from 0 through 1000, with 1000 indicating 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, while 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 require other identity evidence and are outside this text-similarity profile
Similarity scores express evidence, not identity proof; actual text and pixel comparison still runs on each paired page, and no source document is changed
BuildPdfDiffTextChanges reports zero-based UTF-16 offsets and lengths; character mode keeps surrogate pairs together and word mode groups whitespace separately from other characters
BuildPdfDiffPixelRegions groups connected changed tiles into exclusive-right/bottom pixel rectangles; these regions can include unchanged pixels inside a tile
Alignment accepts at most 4,096 pages per side and 16,777,216 cells; text accepts at most 65,536 tokens and 16,777,216 UTF-16 units, with explicit truncation when selected detail budgets are exhausted
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;