Sample code for 30+ languages & platforms
Delphi ActiveX

PDF Update or Add XML Metadata

Demonstrates how to add or update the XML metadata stored in a PDF.

Note: This example requires Chilkat v10.1.0 or later.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
pdf: TChilkatPdf;
xml: TChilkatXml;
sbExisting: TChilkatStringBuilder;
hasMetadata: Integer;
dt: TCkDateTime;
ts: WideString;
sbDocId: TChilkatStringBuilder;
sbInstanceId: TChilkatStringBuilder;
sb: TChilkatStringBuilder;

begin
success := 0;

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

pdf := TChilkatPdf.Create(Self);

// Load a PDF file.
// If the PDF file already has metadata, we'll update it.
// Otherwise this example adds the metadata.
success := pdf.LoadFile('qa_data/pdf/blank_with_metadata.pdf');
if (success = 0) then
  begin
    Memo1.Lines.Add(pdf.LastErrorText);
    Exit;
  end;

xml := TChilkatXml.Create(Self);
sbExisting := TChilkatStringBuilder.Create(Self);

hasMetadata := pdf.GetMetadata(sbExisting.ControlInterface);
if (hasMetadata = 1) then
  begin
    xml.LoadSb(sbExisting.ControlInterface,1);
  end
else
  begin

    // Otherwise create the bare minimum XMP metadata.
    xml.Tag := 'x:xmpmeta';
    xml.AddAttribute('xmlns:x','adobe:ns:meta/');
    xml.AddAttribute('x:xmptk','Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 ');
    xml.UpdateAttrAt('rdf:RDF',1,'xmlns:rdf','http://www.w3.org/1999/02/22-rdf-syntax-ns#');
    xml.UpdateAttrAt('rdf:RDF|rdf:Description',1,'rdf:about','');
    xml.UpdateAttrAt('rdf:RDF|rdf:Description',1,'xmlns:xmp','http://ns.adobe.com/xap/1.0/');
    xml.UpdateAttrAt('rdf:RDF|rdf:Description',1,'xmlns:dc','http://purl.org/dc/elements/1.1/');
    xml.UpdateAttrAt('rdf:RDF|rdf:Description',1,'xmlns:xmpMM','http://ns.adobe.com/xap/1.0/mm/');
    xml.UpdateAttrAt('rdf:RDF|rdf:Description',1,'xmlns:pdf','http://ns.adobe.com/pdf/1.3/');
    xml.UpdateAttrAt('rdf:RDF|rdf:Description',1,'xmlns:xmpRights','http://ns.adobe.com/xap/1.0/rights/');

    dt := TCkDateTime.Create(Self);
    dt.SetFromCurrentSystemTime();
    ts := dt.GetAsTimestamp(1);

    xml.UpdateChildContent('rdf:RDF|rdf:Description|xmp:ModifyDate',ts);
    xml.UpdateChildContent('rdf:RDF|rdf:Description|xmp:CreateDate',ts);
    xml.UpdateChildContent('rdf:RDF|rdf:Description|xmp:MetadataDate',ts);

    xml.UpdateChildContent('rdf:RDF|rdf:Description|xmp:CreatorTool','My Custom Application');
    xml.UpdateChildContent('rdf:RDF|rdf:Description|dc:format','application/pdf');

    sbDocId := TChilkatStringBuilder.Create(Self);
    sbDocId.Append('uuid:');
    sbDocId.AppendUuid(1);
    xml.UpdateChildContent('rdf:RDF|rdf:Description|xmpMM:DocumentID',sbDocId.GetAsString());

    sbInstanceId := TChilkatStringBuilder.Create(Self);
    sbInstanceId.Append('uuid:');
    sbInstanceId.AppendUuid(1);
    xml.UpdateChildContent('rdf:RDF|rdf:Description|xmpMM:InstanceID',sbInstanceId.GetAsString());

  end;

// Add our custom metadata tags to either the existing XML metdata, or the newly created metadata.
xml.UpdateAttrAt('rdf:RDF|rdf:Description',1,'xmlns:zf','urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#');
xml.UpdateAttrAt('rdf:RDF|rdf:Description',1,'rdf:about',' ');
xml.UpdateChildContent('rdf:RDF|rdf:Description|zf:ConformanceLevel','BASIC');
xml.UpdateChildContent('rdf:RDF|rdf:Description|zf:DocumentFileName','ZZZZZZ-invoice.xml');
xml.UpdateChildContent('rdf:RDF|rdf:Description|zf:DocumentType','INVOICE');
xml.UpdateChildContent('rdf:RDF|rdf:Description|zf:Version','1.0');

// Create a new PDF with the updated metadata.
sb := TChilkatStringBuilder.Create(Self);
xml.GetXmlSb(sb.ControlInterface);
success := pdf.UpdateMetadata(sb.ControlInterface,'c:/temp/qa_output/out.pdf');
if (success = 1) then
  begin
    Memo1.Lines.Add('Success');
  end
else
  begin
    Memo1.Lines.Add(pdf.LastErrorText);
  end;

// To see the metadata in Adobe Acrobat DC,
// 1) Open the PDF.
// 2) CTRL-D to show the Document Properties dialog.
// 3) In the dialog, click on the "Additional Metadata" button.
// 4) In the Additional Data dialog, click on "Advanced"
// 5) Expand the namespace tree for the metadata you added.

Memo1.Lines.Add('OK');
end;