PDFium Delphi Component Docs

文件、註解與表單語意比較

v3.265.0 起可在 Small Business 與 Enterprise 版使用,單元位於 Lib\SmallBusiness\FPdfSemanticDiff.pas,由 PDFiumPasSB / PDFiumLazSB 與 FPdfSmallBusiness 承載;公開單元名稱與 namespace 不變。手動升級時請移除殘留的 Enterprise 原始碼副本;請見 版本對照表

ComparePdfSemantics 比較註解與 AcroForm Widget 屬性,不依賴頁面繪製;即使外觀像素完全相同,表單值也可能已變更

傳入頁面對齊產生的 TPdfDiffPagePairs 或明確對應;nil 使用依位置配對,明確對應則必須將兩份文件的每頁各涵蓋一次,缺少的一側使用 -1

註解識別使用 /NM 名稱,表單 Widget 識別使用完整欄位名稱;識別範圍限定於對應頁面及項目種類

缺少名稱或重複名稱,包括同頁上同一欄位的多個 Widget,會回報 psckAmbiguous 與明確原因;註解陣列順序不決定識別

支援模式比較字串、旗標、色彩、不透明度、邊框、標記、線段、頂點與墨跡幾何、URI 動作、欄位值、選項標籤與選定選項,以及核取方塊或選項按鈕狀態

選用的文件中繼資料與本地目的地

CompareDocumentMetadata 預設 False,會比較標準 Info 值 Title、Author、Subject、Keywords、Creator、Producer、CreationDate 與 ModDate;缺席值與空值視為相等,日期字串逐字比較

CompareXmpMetadata 預設為 False,將解析後 XMP 封包中具命名空間限定的屬性作為每側一個 psikXmp 項目進行比較;bag、sequence 與語言替代容器會攤平為每個屬性一個有界值,僅單側存在的封包會將其所有屬性回報為新增或移除,填充位元組、封包包裝位元組與空白會被忽略

CompareOutlines 預設為 False,會以斜線分隔的標題路徑為鍵,把文件書籤階層比較為 psikOutline 項目;擷取的屬性包括 Title、Depth、IsOpen、ChildCount、Style、Color、對應的本機 DirectDestination./ActionDestination. 目標、ActionUri 與 ActionFilePath

CompareAttachments 預設為 False,會以檔名為鍵,把文件附件樹中的內嵌檔案比較為 psikAttachment 項目;擷取的屬性包括 Size、小寫十六進位的 Sha256 酬載摘要、Subtype、Description、AFRelationship 與字串型 /Params 中繼資料

ComparePageAttributes 預設為 False,會為每個對應的頁面比較頁面層級的幾何、旋轉與標籤,作為一個 psikPage 項目(識別 Page);擷取的屬性包括 MediaBox、CropBox、BleedBox、TrimBox、ArtBox(在 GeometryTolerance 容差下比較)、Rotation(0、90、180 或 270)與 PageLabel

CompareNamedDestinations 預設為 False,會以目的地名稱為鍵,把 /Names /Dests 與舊式 /Dests 中的文件層級具名目的地比較為 psikNamedDestination 項目,擷取對應的 Destination.TargetPair、Destination.View 與 GeometryTolerance 容差下的 fit 參數,並把變更計入 NamedDestinationChangeCount

CompareStructureTree 預設為 False,會把邏輯結構樹(/StructTreeRoot)比較為 psikStructure 項目:有元素 ID 時以 ID:<ElementID> 為鍵,否則以樹狀路徑 Root[i]/Child[j]... 為鍵;擷取的屬性包括 RoleName、Depth、ChildCount、ContentItemCount、Title、AlternateText、ActualText、ExpansionText、Language、ElementID、ClassName、TableScope、TableHeaders、TableRowSpan、TableColSpan、ListNumbering、LayoutPlacement 與 LayoutBBox,並把變更計入 StructureChangeCount

CompareOptionalContent 預設為 False,會以圖層 Name 為鍵,把選用內容群組(/OCProperties /OCGs)比較為 psikOptionalContent 項目;擷取的屬性包括 Intents、ViewState、PrintState、ExportState、EffectiveVisible、Locked、RadioGroupIndex、PageElementSubtype、Language、LanguagePreferred、MinimumZoom 與 MaximumZoom,並把變更計入 OptionalContentChangeCount

除了 LinkUri 與本機目的地之外,頁面連結註解還會為 GoToR 動作擷取 ActionFilePath、RemoteDestination.PageNumber 與 RemoteDestination.Suffix,為 Launch 動作擷取 ActionFilePath,並為 /S /Named 動作擷取 NamedAction(NextPage、PrevPage、FirstPage、LastPage)

CustomAnnotationKeys 為每個符合的註解最多命名 64 個要比較的註解字典鍵,作為 Custom:<key> 屬性:字串值按字面比較,數值按數值比較,其他值類型僅回報存在性;單側缺少的鍵回報為新增或移除

中繼資料變更使用 psikDocument,識別為 Document,頁面/註解索引為 -1 且 HasBounds 為 False;它們不產生疊圖矩形,並讓 ComparedItemCount 與共用的 MaxItems 預算各側加一

CompareLinkDestinations 預設 False,為連結註解加入直接 /Dest 與本地 GoTo 目的地;具名目的地透過 PDFium 解析,遠端文件絕不開啟

DirectDestination. 與 ActionDestination. 屬性包含 TargetPair、View、ParameterCount、可索引的 Parameter[n] 值,XYZ 檢視另有 HasX、HasY 與 HasZoom;TargetPair 是已驗證頁面配對陣列中的索引,因此移動的頁面仍保留其識別

檢視參數使用 GeometryTolerance;XYZ 的 null 座標會與明確零值區分,非 XYZ 的 null 對零區分則遵循 PDFium 公開的檢視值

無法解析的本地目標與不一致的原生讀取會拒絕完整報告;所有新值都使用既有的字串、項目、變更與比較預算

CompareAppearanceStreams 可選擇比較註解外觀串流內容;預設停用

CompareAnnotations 與 CompareForms 分別選取對應項目種類;GeometryTolerance 預設為 0.001 PDF 使用者空間點

新增、移除與變更項目包含變更前後的屬性與邊界;BuildSemanticDiffOverlays 分別傳回變更前後矩形,並附上頁面與變更索引

所有索引從零起算,邊界維持在以點表示的 PDF 使用者空間;繪製到點陣圖之前,請套用相應頁面的旋轉、裁切與視埠變換

頁面使用獨立原生 Handle 載入,保留呼叫端目前頁面與表單焦點,避免觸發表單動作

此模式不涵蓋非 Widget 欄位、任意自訂字典、內嵌檔案、指令碼動作、遠端目的地內容、XMP 中繼資料與自訂 Info 鍵;明確拒絕 XFA 表單比較

預設上限為 10,000 組頁面配對、4,096 個掃描項目、4,096 項變更、每個字串 1 MiB、字串總計 16 MiB 及 16,777,216 次比較;預算耗盡時拒絕不完整輸出

Complete 區分成功且完整的比較與遭拒絕的比較;變更可能包含明確的識別歧義,因此將結果視為確定的項目匹配之前,請先檢查 AmbiguousCount

TPdfSemanticDiffReport 傳回 Changes 陣列與各類別總計,並在 ComparedItemCount 記錄從兩份文件讀取的項目數;比較遭拒絕時,ErrorMessage 攜帶拒絕原因

TPdfSemanticDiffOptions 保存比較開關、GeometryTolerance,以及掃描開始前完成驗證的預算上限;其 Default 類別函式回傳上述預設值

TPdfSemanticItemKind 將 psikAnnotation 一般註解與 psikFormWidget AcroForm widget 分開;psikDocument 與 psikXmp 保存選用的文件 metadata 快照,而 psikOutline、psikAttachment、psikPage、psikNamedDestination、psikStructure 與 psikOptionalContent 則保存選用的書籤、內嵌檔案、頁面屬性、具名目的地、邏輯結構與 optional content 快照;識別比對絕不跨種類進行

TPdfSemanticItem 為單一受比對項目建立快照,含其 Kind、Identity、從零起算的 PageIndex 與 AnnotationIndex、註解矩形的 HasBounds 與 Bounds,以及擷取的 Properties

TPdfSemanticProperty 以 Name 與 Value 配對記錄一項擷取到的事實,TPdfSemanticProperties 則是為項目擷取的事實動態陣列

TPdfSemanticChangeKind 把每個結果分類為 psckAdded、psckRemoved、psckChanged 或 psckAmbiguous;沒有存留屬性差異的變更條目會被捨棄

TPdfSemanticPropertyChange 以 Name 識別一個不同的屬性,搭配 HasBefore 與 HasAfter 存在旗標以及 BeforeValue 與 AfterValue 值;TPdfSemanticPropertyChanges 是其動態陣列

TPdfSemanticChange 描述一項結果,含其 Kind、項目的 ItemKind 與 Identity、變更前後的項目快照、Properties 中的逐屬性差異,以及歧義識別攜帶的 AmbiguityReason 文字;TPdfSemanticChanges 是其動態陣列

TPdfSemanticSide 為疊加矩形選擇 pssBefore 或 pssAfter 文件

TPdfSemanticOverlay 標記一個矩形,含其 Side、來源的從零起算 PageIndex、指向 Changes 的 ChangeIndex、變更的 Kind,以及 PDF 使用者空間中的 Bounds;TPdfSemanticOverlays 是其動態陣列

實際 Delphi/FPC Win32/Win64 驗收每個目標涵蓋 92 項斷言,包括儲存/重新開啟文件、相同像素下的值變更、Unicode 識別、頁面移動、歧義及預算

公開宣告

type
  TPdfSemanticItemKind = (psikAnnotation, psikFormWidget, psikDocument,
    psikXmp, psikOutline, psikAttachment, psikPage, psikNamedDestination,
    psikStructure, psikOptionalContent);
  TPdfSemanticChangeKind = (psckAdded, psckRemoved, psckChanged, psckAmbiguous);
  TPdfSemanticSide = (pssBefore, pssAfter);
  TPdfSemanticProperty = record
    Name: string;
    Value: WideString;
  end;
  TPdfSemanticProperties = array of TPdfSemanticProperty;
  TPdfSemanticCustomKeys = array of string;
  TPdfSemanticItem = record
    Kind: TPdfSemanticItemKind;
    Identity: WideString;
    PageIndex, AnnotationIndex: Integer;
    HasBounds: Boolean;
    Bounds: TPdfRectangle;
    Properties: TPdfSemanticProperties;
  end;
  TPdfSemanticPropertyChange = record
    Name: string;
    HasBefore, HasAfter: Boolean;
    BeforeValue, AfterValue: WideString;
  end;
  TPdfSemanticPropertyChanges = array of TPdfSemanticPropertyChange;
  TPdfSemanticChange = record
    Kind: TPdfSemanticChangeKind;
    ItemKind: TPdfSemanticItemKind;
    Identity: WideString;
    HasBefore, HasAfter: Boolean;
    BeforeItem, AfterItem: TPdfSemanticItem;
    Properties: TPdfSemanticPropertyChanges;
    AmbiguityReason: string;
  end;
  TPdfSemanticChanges = array of TPdfSemanticChange;
  TPdfSemanticOverlay = record
    Side: TPdfSemanticSide;
    PageIndex, ChangeIndex: Integer;
    Kind: TPdfSemanticChangeKind;
    Bounds: TPdfRectangle;
  end;
  TPdfSemanticOverlays = array of TPdfSemanticOverlay;
  TPdfSemanticDiffOptions = record
    CompareAnnotations, CompareForms, CompareAppearanceStreams: Boolean;
    CompareDocumentMetadata, CompareLinkDestinations: Boolean;
    CompareXmpMetadata: Boolean;
    CompareOutlines, CompareAttachments, ComparePageAttributes: Boolean;
    CompareNamedDestinations, CompareStructureTree, CompareOptionalContent: Boolean;
    CustomAnnotationKeys: TPdfSemanticCustomKeys;
    GeometryTolerance: Double;
    MaxPagePairs, MaxItems, MaxChanges, MaxStringBytes: Integer;
    MaxTotalStringBytes, MaxComparisons: Int64;
    class function Default: TPdfSemanticDiffOptions; static;
  end;
  TPdfSemanticDiffReport = record
    Complete: Boolean;
    Changes: TPdfSemanticChanges;
    AddedCount, RemovedCount, ChangedCount, AmbiguousCount: Integer;
    NamedDestinationChangeCount, StructureChangeCount, OptionalContentChangeCount: Integer;
    ComparedItemCount: Integer;
    ErrorMessage: string;
  end;

// Indices are zero based. Nil PagePairs selects positional pairing. Explicit
// pairs must cover both documents exactly once; -1 denotes an absent side
// Identity is scoped to a mapped page and item kind. Missing names and repeated
// names (including repeated widgets of one field) are reported as ambiguous
// No rendering, focus changes, form actions, or positional identity guessing
// are performed. Pages are opened independently of the caller's current page
// The supported profile covers strings, flags, colors, opacity, borders,
// markup/line/vertex/ink geometry, URI actions, field values/options/checks,
// and optionally appearance streams. Arbitrary unconfigured dictionaries,
// script actions and non-widget fields are not compared. Optional metadata
// covers standard Info tags and optional namespace-qualified XMP packet
// properties; optional local destinations use mapped page IDs
function ComparePdfSemantics(BeforePdf, AfterPdf: TPdf;
  const PagePairs: TPdfDiffPagePairs; const Options: TPdfSemanticDiffOptions;
  out Report: TPdfSemanticDiffReport): Boolean;

// Bounds remain in PDF user space, in points. Transform with the destination
// page's rotation/crop/viewport rather than treating them as bitmap pixels
function BuildSemanticDiffOverlays(const Report: TPdfSemanticDiffReport):
  TPdfSemanticOverlays;