PDFium Delphi Component Docs

文档、注解与表单语义比对

自 v3.265.0 起随 Lib\SmallBusiness\FPdfSemanticDiff.pas、PDFiumPasSB / PDFiumLazSB 与 FPdfSmallBusiness 提供于 Small Business 与 Enterprise 版;公开单元名与 namespace 保持不变。手动升级时请移除残留的 Enterprise 源副本;参见版本矩阵

ComparePdfSemantics 独立于页面渲染比对注解和 AcroForm widget 属性;即使外观像素完全相同,表单值也可能变化

传入页面对齐得到的 TPdfDiffPagePairs 或显式映射;nil 使用位置配对,显式映射须恰好覆盖两个文档各一次,缺失的一侧使用 -1

注解以 /NM 名称标识,表单 widget 以字段全限定名标识;身份范围限定在映射页面和项类型内

缺失或重复名称会报告为 psckAmbiguous 并给出原因,同一字段在同页有多个 widget 也属于此类;注解数组顺序不决定身份

受支持配置比对字符串、标志、颜色、不透明度、边框、标记、线段/顶点/墨迹几何、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 和 fit 参数(在 GeometryTolerance 下),变更计入 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,把可选内容组(/OCProperties /OCGs)作为 psikOptionalContent 项比对,以图层 Name 为键;捕获属性包括 Intents、ViewState、PrintState、ExportState、EffectiveVisible、Locked、RadioGroupIndex、PageElementSubtype、Language、LanguagePreferred、MinimumZoom 与 MaximumZoom,变更计入 OptionalContentChangeCount

页面链接注解为 GoToR 动作捕获 ActionFilePath、RemoteDestination.PageNumber 与 RemoteDestination.Suffix,为 Launch 动作捕获 ActionFilePath,为 /S /Named 动作捕获 NamedAction(NextPage、PrevPage、FirstPage、LastPage),此外还有 LinkUri 与本地目标

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 用户空间坐标;绘制到位图前需应用相应页面旋转、裁剪和视口变换

页面使用独立原生句柄加载,保留调用方当前页及表单焦点,并避免触发表单动作

该配置不涵盖非 widget 字段、未经配置的任意自定义字典、脚本动作、远程目标内容和自定义 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 与 AcroForm widget psikFormWidget 区分开;psikDocument 与 psikXmp 承载可选的文档元数据快照,而 psikOutline、psikAttachment、psikPage、psikNamedDestination、psikStructure 与 psikOptionalContent 承载可选的书签、嵌入文件、页面属性、命名目标、逻辑结构和可选内容快照;身份匹配不会跨越项类型

TPdfSemanticItem 快照一个参与比对的条目,含 Kind、Identity、基于 0 的 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、来源页基于 0 的 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;