PDFium Delphi Component Docs

SaveAsOptionalContentCreated method

Component: TPdf  ·  Unit: PDFium
Creates new optional-content groups, membership dictionaries and OC-gated Form XObject layer content in a strictly verified copy

Syntax

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;

Description

The method stages the current document through PDFium, appends one optional-content group dictionary per Groups entry, registers every created group in the catalogue /OCProperties default configuration (/OCGs, /ON, /OFF, /Order, /Locked, and one /RBGroups array per shared RadioTag), and creates the whole optional-content dictionary when the document has none

Each Memberships entry appends an /OCMD dictionary with the referenced group object numbers plus either an /AllOn or /AnyOn policy or a one-level or nested /VE visibility expression tree (up to 32 levels via Expression.Children) written as /And (poceAll), /Or (poceAny) or /Not (poceNot) per ISO 32000-1 ยง8.11.2.2

Each Forms entry embeds a Form XObject whose stream bytes are supplied by the caller, writes either an empty /Resources << >> dictionary or an inherited snapshot of the target page's /Resources dictionary when InheritPageResources is True, references the gating group through its /OC entry, registers the form under ResourceName (default FmOc1, FmOc2, ...) in the page resources, and appends one /Name Do invocation in a new page content stream so PDFium's native optional-content context gates the drawing

The result is reopened through strict validation, and every created group is read back through InspectOptionalContent to confirm its name, initial state and locked registration before the output is delivered

Safety

Example

var
  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;

See Also

SaveAsOptionalContentConfigured, InspectOptionalContent, RenderPageByIndex