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

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

OptionDefaultScope
MaxPages500Each document
MaxParagraphs4000Each document after exclusions
MaxSemanticLines50000Combined semantic lines before snapshots and exclusions
MaxCharacters2000000Combined extracted line text before exclusions
MaxTokens200000Combined word and, in sentence mode, sentence tokens
MaxDifferences10000Returned records, including moves
MaxAlignmentCells2000000Cumulative changed-paragraph token matrices
MaxWorkUnits50000000Extraction 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