Contract Comparison
HPDFContractCompare compares the semantic text and line geometry of two loaded PDF documents, aligns paragraphs and returns auditable insert, delete, replace and move records
Use this helper for wording and clause review, and use THPDFDocComparison for page, object and rendered differences
Compare loaded contracts
uses HPDFDoc, HPDFTypes, HPDFContractCompare;
Options := HPDFDefaultContractCompareOptions;
Options.Granularity := ccgWord;
BeforePDF.LoadFromFile('contract-before.pdf');
AfterPDF.LoadFromFile('contract-after.pdf');
if HPDFCompareContracts(BeforePDF, AfterPDF, Options, Report, Token) then
begin
HTML := HPDFContractReviewHTML(Report, 'Services agreement review',
8388608, Token);
HPDFSaveContractAnnotatedPDF('contract-before.pdf',
'contract-before-review.pdf', Report, ccsBefore, Token);
HPDFSaveContractAnnotatedPDF('contract-after.pdf',
'contract-after-review.pdf', Report, ccsAfter, Token);
end;
Save the returned HTML using UTF-8, and inspect Report.Status and Report.Note when the comparison function returns false
Paragraph alignment and changes
- Semantic lines are grouped using page, column, list or heading boundaries, and configurable baseline gaps, with whitespace normalised between lines
- Exact paragraphs are paired first, and repeated identical body clauses retain occurrence order and multiplicity
- Changed paragraphs are matched by a configurable multiset word similarity score, independent of the chosen difference granularity
- A longest increasing sequence of paired paragraphs identifies reordering without reporting ordinary index shifts from insertions and deletions as moves
- Changed matched paragraphs use a bounded token alignment to return insert, delete or replace records with both full paragraph contexts
- Word mode separates punctuation from words, and sentence mode uses a terminal full stop, exclamation mark or question mark followed by whitespace or the end of the paragraph
- Sentence boundaries are lexical and can split abbreviations or numbered clause labels, so word mode is the default for precise legal wording review
JoinPageContinuationsconservatively joins an unfinished paragraph near the bottom of one page to a plain paragraph near the top of the next page, and callers can disable this layout heuristic
Page and geometry references
PageIndex and ParagraphIndex are zero-based, whilst CharacterStart is a one-based UTF-16 position within the whitespace-normalised paragraph
An absent side of a whole-paragraph insertion or deletion has page and paragraph indices of -1, and a token insertion or deletion within a matched paragraph retains a zero-length position on the opposite side
Each rectangle carries its own page index and covers an intersecting semantic line in PDF user space, and a paragraph spanning pages can have rectangles on several pages
Locations describe line coverage rather than exact character quadrilaterals, so an annotated PDF can highlight the same line for several independent word changes
Exclude recurring page furniture and boilerplate
The defaults exclude matching semantic lines that occur in the top or bottom 54 points on at least two distinct pages of each input, and normalise margin digits so repeated page numbers can match
The before and after documents are filtered independently, and body repetitions remain eligible for comparison regardless of how many times they occur
Options.IgnoreRepeatedHeadersFooters := False;
Options.NormalizeMarginDigits := False;
Options.HeaderMarginPoints := 40;
Options.FooterMarginPoints := 35;
SetLength(Options.ExcludedLineTexts, 1);
Options.ExcludedLineTexts[0] :=
'The parties shall maintain an accurate register of approved subcontractors.';
ExcludedLineTexts removes a complete semantic line by literal equality after whitespace normalisation, respects IgnoreCase and never interprets regular expressions or substring patterns
Choose margins and exclusions deliberately, because dates or other contractual text inside an excluded recurring margin line are omitted from the wording review
Budgets and cancellation
| Option | Default | Scope |
|---|---|---|
MaxPages | 500 | Each document |
MaxParagraphs | 4000 | Each document after exclusions |
MaxSemanticLines | 50000 | Combined semantic lines before snapshots and exclusions |
MaxCharacters | 2000000 | Combined extracted line text before exclusions |
MaxTokens | 200000 | Combined word and, in sentence mode, sentence tokens |
MaxDifferences | 10000 | Returned records, including moves |
MaxAlignmentCells | 2000000 | Cumulative changed-paragraph token matrices |
MaxWorkUnits | 50000000 | Extraction accounting and alignment work |
A budget limit returns ccsBudgetExceeded, and cancellation returns ccsCancelled, with partial findings retained for diagnosis and marked incomplete in HTML
Cancellation is checked between page extractions and throughout library-owned alignment loops, whilst the underlying single-page semantic extractor remains synchronous
ccsInvalidInput reports missing documents, invalid enum ordinals or invalid options, and ccsExtractionFailed reports pages with no extractable text or failed semantic extraction
OCR or explicit blank-page removal is required before comparing pages with no extractable text, and ccsComplete certifies completion of this text comparison only
Review artefacts
HPDFContractReviewHTML returns a standalone document with escaped title, messages, text, full paragraph contexts, change kinds and both source locations, with an output limit of 8388608 UTF-16 characters by default
The HTML exporter raises EHPDFContractCompareBudget when its output exceeds the configured limit, and its cancellation token raises EHPDFOperationCancelled
HPDFSaveContractAnnotatedPDF requires a completed report, loads a separate source document, and checks every semantic page snapshot including its exact line geometry before adding highlights with numbered review comments
Specify distinct source and output paths, select ccsBefore or ccsAfter, and export both versions when you need to review whole-paragraph deletions and insertions together
On Windows, source and output file identities are checked to reject hard-link or other aliases of the source file
The PDF exporter disables AutoLaunch, leaves the input documents and source file unchanged, and checks cancellation before loading, between pages and changes, and before saving
This helper compares extracted wording and layout references without inferring legal meaning, recognising scanned text or certifying the legal effect of a revision
Related APIs
HPDFCompareContracts · THPDFContractCompareOptions · THPDFContractCompareReport · HPDFContractReviewHTML · HPDFSaveContractAnnotatedPDF