PDFium Delphi Component Docs

FPdfXmp unit

Unit: FPdfXmp
Provides a bounded namespace-aware XMP property tree and PDF metadata stream helpers

Core API

constructor TPdfXmpPacket.Create;
class function TPdfXmpPacket.Parse(const Xml: TPdfXmpString): TPdfXmpPacket;
class function TPdfXmpPacket.ParseUtf8(const Data: TBytes): TPdfXmpPacket;
class function TPdfXmpPacket.ParseBytes(const Data: TBytes): TPdfXmpPacket;
class function TPdfXmpPacket.ParseBytesWithSourcePrefixes(const Data: TBytes): TPdfXmpPacket;
function PdfXmpPacketEncoding(const Data: TBytes): TPdfXmpPacketEncoding;
function PdfXmpPacketText(const Data: TBytes): TPdfXmpString;
function PdfXfaConfigRequiresDynamicRender(const Data: TBytes): Boolean;
function TPdfXmpPacket.Clone: TPdfXmpPacket;
function TPdfXmpPacket.TryGetSimpleValue(const NamespaceUri, Name: TPdfXmpString; out Value: TPdfXmpString): Boolean;
procedure TPdfXmpPacket.SetSimpleValue(const NamespaceUri, PreferredPrefix, Name, Value: TPdfXmpString);
procedure TPdfXmpPacket.SetArrayValues(const NamespaceUri, PreferredPrefix, Name: TPdfXmpString; ContainerKind: TPdfXmpContainerKind; const Values: array of TPdfXmpString);
procedure TPdfXmpPacket.SetLanguageAlternative(const NamespaceUri, PreferredPrefix, Name, Language, Value: TPdfXmpString);
function TPdfXmpPacket.ToXml(PaddingLength: Integer = -1): TPdfXmpString;
function TPdfXmpPacket.ToUtf8(PaddingLength: Integer = -1): TBytes;
function ReadPdfXmpPacket(const PdfData: TBytes; out PacketData: TBytes): Boolean;
function ParsePdfXmpPacket(const PdfData: TBytes): TPdfXmpPacket;
function WritePdfXmpPacketIncremental(const PdfData: TBytes; Packet: TPdfXmpPacket): TBytes;
function ComparePdfAInfoAndXmp(const Info: TPdfAMetadataValues; Packet: TPdfXmpPacket): TPdfAMetadataReport;
function SynchronizePdfAInfoToXmp(const Info: TPdfAMetadataValues; Packet: TPdfXmpPacket): TPdfAMetadataFields;
function TPdf.ReadXmpPacket: TPdfXmpPacket;
function TPdf.InspectPdfAMetadata: TPdfAMetadataReport;
function TPdf.NormalizePdfAMetadata(Stream: TStream; AllowSignedDocument: Boolean = False): Boolean;
function TPdf.SaveAsWithXmpPacket(Stream: TStream; Packet: TPdfXmpPacket; AllowSignedDocument: Boolean = False): Boolean;

Value model

TypeDescription
EPdfXmpErrorException raised for malformed, unsafe, or unsupported XMP and PDF metadata input
TPdfXmpString / TPdfXmpStringsUnicode XMP string and its dynamic-array form
TPdfXmpContainerKindpxckSimple for a simple property, pxckBag for an unordered bag, pxckSequence for an ordered sequence, pxckAlternative for a language alternative, or pxckResource for a nested resource
TPdfXmpValueKindValue kind of pxvkText, pxvkUri, or pxvkResource for a preserved nested RDF resource
TPdfXmpNamespace / TPdfXmpNamespacesNamespace Prefix and Uri pair and its dynamic-array form
TPdfXmpQualifier / TPdfXmpQualifiersQualifier with NamespaceUri, Prefix, Name, and Value fields and its dynamic-array form
TPdfXmpValue / TPdfXmpValuesTyped value with Kind, Text, Language, Qualifiers, RawXml, and ParseType fields and its dynamic-array form. ParseType keeps the source rdf:parseType verbatim (Resource, Literal, Collection) so a read-modify-write round trip preserves it; when it is empty, the writer infers rdf:parseType="Resource" for structured values whose raw markup starts with property elements
TPdfXmpProperty / TPdfXmpPropertiesProperty with NamespaceUri, Prefix, Name, ContainerKind, Qualifiers, and Values fields and its dynamic-array form
TPdfXmpPacketEncodingEncoding an XMP packet arrived in: xpeUtf8, xpeUtf16Le, xpeUtf16Be, xpeUtf32Le or xpeUtf32Be, as classified by PdfXmpPacketEncoding. A PDF/A-1 metadata packet may use any of them; a PDF/A-4 one has to be UTF-8
TPdfXmpValueClassLexically constrained value type of a predefined property: pxvcText for every unconstrained string type, pxvcInteger, pxvcReal, pxvcRational, pxvcDate, or pxvcBoolean
TPdfXmpPropertyShapeContainer form a predefined property is declared with: pxpsSimple, pxpsBag, pxpsSequence, pxpsAlternative, or pxpsResource for a structured value
TPdfXmpPredefinedTypeDeclaration of one predefined property: NamespaceUri, Name, Shape, Value, and LangAlt, which marks the language alternatives whose every item must carry xml:lang
TPdfAMetadataValuesPDFium-decoded values plus an explicit presence set for the eight Info keys mapped by ISO 19005-1
TPdfAMetadataComparison / TPdfAMetadataReportPer-field RDF type, value, and date evidence plus the aggregate mismatch set and XMP parse status

TPdfXmpPacket Members

MemberDescription
CreateCreates an empty packet using safe wrapper and padding defaults
Parse / ParseUtf8Parses Unicode XML or UTF-8 bytes into a bounded property tree
ParseBytesParses a packet in the encoding it declares — UTF-8, UTF-16 or UTF-32, either byte order, with or without a byte order mark — by reading the mark or the NUL pattern of the leading token; PdfXmpPacketEncoding and PdfXmpPacketText expose the classification and the decoded text for callers that scan a packet rather than parse it
ParseBytesWithSourcePrefixesDecodes the same bounded multi-encoding packet as ParseBytes, but each parsed property keeps the prefix exactly as written in the source instead of the packet's registered namespace prefix; use it to inspect original property names before namespace normalisation
CloneReturns an independent deep copy of the packet
NamespaceCount / PropertyCountReturn the number of registered namespaces or properties
TryGetPropertyFinds a property by namespace URI and local name and returns its index
TryGetSimpleValueReturns the text of a simple property when present
RegisterNamespaceRegisters a preferred prefix for a namespace URI whilst avoiding prefix collisions
RemovePropertyRemoves the matching namespace-aware property
SetSimpleValueCreates or replaces a scalar text property
SetArrayValuesCreates or replaces a bag, sequence, or alternative from text values
SetLanguageAlternativeAdds or replaces one language-tagged value in an alternative container
ToXml / ToUtf8Serialises the packet as Unicode XML or UTF-8 bytes with optional padding
HasPacketWrapperControls whether serialisation emits the XMP packet processing-instruction wrapper
Namespaces / PropertiesExpose the packet namespace and property arrays for inspection
PacketEndMode / PacketIdExpose the packet wrapper end mode and packet identifier
PaddingLengthGets or sets the preserved default serialisation padding length

Predefined Property Table

FunctionDescription
PdfXmpPredefinedTypeLooks up the declaration of a property of a predefined schema by namespace URI and local name, reporting whether the schema defines it at all. The optional PartNo selects the specification that names the declaration, since the XMP specification of 2004 and ISO 16684-1 do not always agree: part 1 reads the earlier one, and the default of 2 gives the current view that ISO 19005-2 6.6.2.3.1 and ISO 19005-3 defer to
PdfXmpValueMatchesClassTests a value against a value class: an optionally signed decimal integer, decimal notation, a numerator/denominator rational, an ISO 8601 date, or True / False
PdfXmpShapeMatchesTests a parsed container kind against a declared shape

PDF Byte Helpers

FunctionDescription
ReadPdfXmpPacketExtracts the metadata stream bytes from a complete PDF byte array
ParsePdfXmpPacketExtracts and parses the metadata packet, returning nil when none exists
WritePdfXmpPacketIncrementalReturns PDF bytes with a new metadata stream and catalogue update appended as an incremental revision
ComparePdfAInfoAndXmpCompares all present ISO 19005-1 Table 1 Info entries against their typed XMP properties
SynchronizePdfAInfoToXmpRepairs only mismatched mapped properties and returns the fields it changed

Behaviour

Unknown namespace declarations, property namespaces, qualifiers, RDF bags, sequences, alternatives, URI resources, nested resources, xml:lang values, packet IDs, end modes, and padding round-trip through the property tree

Property and array item qualifiers are written with prefixes bound to their namespace URIs. Unprefixed qualifiers keep an empty namespace even under a default element namespace

Packet wrapper metadata is taken from parsed xpacket processing instructions with exact attribute names; wrapper text hiding in comments or CDATA does not count, and read-only end="r" values and padding lengths survive cloning, CRLF input included

Nested resource serialisation keeps text and CDATA in their original order relative to child elements; duplicate attribute names, duplicate expanded names across namespace aliases and undeclared prefixes raise EPdfXmpError

Serialisation assigns collision-free prefixes whilst preserving known prefixes where possible and emits UTF-8 without locale-dependent conversions

ReadXmpPacket returns nil when a document has no metadata stream and rejects incomplete progressive sources

SaveAsWithXmpPacket writes a fresh native document and appends a metadata and catalogue update, then atomically replaces file destinations

PDF/A comparison resolves properties by namespace URI, requires rdf:Alt with x-default for Title and Subject, requires a one-item rdf:Seq for Author, and requires simple values for Keywords, Creator, Producer, CreationDate, and ModDate

Text equality is exact after PDFium decodes Info strings to Unicode; date equality compares matching local values without zones or the same UTC instant when both values carry zones

Synchronisation preserves unrelated properties, unknown namespaces, and non-default language alternatives, and emits canonical UTC XMP dates when an Info date carries a timezone

The predefined property table covers the ten schemas of XMP Specification Part 2 that PDF/A-1 refers to — Dublin Core, XMP Basic, Rights Management, Media Management, Basic Job Ticket, Paged-Text, Adobe PDF, Photoshop, EXIF for TIFF, and EXIF — together with the value class each property is declared with. Auxiliary field schemas such as stRef and stEvt only appear inside a structured value and are therefore not part of it: a structured property keeps its fields as raw XML. InstanceID is declared as a simple property even though PDF/A-1 does not treat its presence as a violation

XFA configuration detection

PdfXfaConfigRequiresDynamicRender reads an XFA configuration packet, either a bare config element or one wrapped in an xdp root, and reports True only when acrobat/acrobat7/dynamicRender carries the text required; it shares the packet-encoding sniffing of the other byte helpers, applies the same decoded-input and allocation budgets, and reports False, rather than raising, for malformed or oversized input

Security and limits

Example

var
  Packet: TPdfXmpPacket;
begin
  Packet := Pdf1.ReadXmpPacket;
  if Packet = nil then
    Packet := TPdfXmpPacket.Create;
  try
    Packet.SetSimpleValue('http://ns.adobe.com/xap/1.0/', 'xmp', 'CreatorTool', 'PDFium Delphi Component');
    Pdf1.SaveAsWithXmpPacket('C:\Output\metadata.pdf', Packet);
  finally
    Packet.Free;
  end;
end;