function SaveAsOptionalContentCreated(Stream: TStream;
const Groups: TPdfOptionalContentCreateGroups;
const Memberships: TPdfOptionalContentCreateMemberships;
const Forms: TPdfOptionalContentCreateForms;
out Report: TPdfOptionalContentCreateReport;
AllowSignedDocument: Boolean = False): Boolean; overload;
function SaveAsOptionalContentCreated(const FileName: string;
const Groups: TPdfOptionalContentCreateGroups;
const Memberships: TPdfOptionalContentCreateMemberships;
const Forms: TPdfOptionalContentCreateForms;
out Report: TPdfOptionalContentCreateReport;
AllowSignedDocument: Boolean = False): Boolean; overload;
该方法让当前文档经过 PDFium 暂存,为 Groups 的每个条目追加一个可选内容组字典,把每个创建的组登记进 catalog 的 /OCProperties 默认配置(/OCGs、/ON、/OFF、/Order、/Locked,以及每个共享 RadioTag 对应的一个 /RBGroups 数组),文档没有可选内容字典时则创建整个字典
Memberships 的每个条目追加一个 /OCMD 字典,内含被引用组的对象号,外加 /AllOn 或 /AnyOn 策略,或一级/嵌套的 /VE 可见性表达式树(经 Expression.Children 最多 32 层),按 ISO 32000-1 §8.11.2.2 写成 /And(poceAll)、/Or(poceAny)或 /Not(poceNot)
Forms 的每个条目嵌入一个 Form XObject,其流字节由调用方提供;InheritPageResources 为 True 时写入目标页面 /Resources 字典的继承快照,否则写入空的 /Resources << >> 字典;通过自身的 /OC 条目引用门控组,在页面资源中以 ResourceName(默认 FmOc1、FmOc2、…)登记表单,并在新的页面内容流中追加一条 /Name Do 调用,由 PDFium 的原生可选内容上下文门控绘制
结果会经过严格验证重新打开,每个创建的组都通过 InspectOptionalContent 读回,确认其名称、初始状态和锁定登记之后才交付输出
/Resources 字典,除非 InheritPageResources=True 快照目标页面直接或继承的 /Resources 字典;直接与间接的页面 /Resources 字典以及直接与间接的 catalog /OCProperties /D 配置都会被解析AllowSignedDocument=Truevar
Groups: TPdfOptionalContentCreateGroups;
Forms: TPdfOptionalContentCreateForms;
Report: TPdfOptionalContentCreateReport;
begin
SetLength(Groups, 1);
Groups[0].Name := 'Watermark';
Groups[0].Intent := pociView;
Groups[0].InitialStateVisible := False;
SetLength(Forms, 1);
Forms[0].GroupIndex := 0;
Forms[0].PageNumber := 1;
Forms[0].Content := TEncoding.ASCII.GetBytes('0 0 1 rg 100 100 200 100 re f');
Forms[0].BBoxRight := 612;
Forms[0].BBoxTop := 792;
Pdf1.SaveAsOptionalContentCreated('C:\Layers.pdf', Groups, nil, Forms, Report);
end;