Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.StringBuilder,
Chilkat.Pdf,
Chilkat.Xml,
Chilkat.CkDateTime;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
pdf: TPdf;
xml: TXml;
sbExisting: TStringBuilder;
hasMetadata: Boolean;
dt: TCkDateTime;
ts: string;
sbDocId: TStringBuilder;
sbInstanceId: TStringBuilder;
sb: TStringBuilder;
begin
success := False;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
pdf := TPdf.Create;
// 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 = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
xml := TXml.Create;
sbExisting := TStringBuilder.Create;
hasMetadata := pdf.GetMetadata(sbExisting);
if (hasMetadata = True) then
begin
xml.LoadSb(sbExisting,True);
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',True,'xmlns:rdf','http://www.w3.org/1999/02/22-rdf-syntax-ns#');
xml.UpdateAttrAt('rdf:RDF|rdf:Description',True,'rdf:about','');
xml.UpdateAttrAt('rdf:RDF|rdf:Description',True,'xmlns:xmp','http://ns.adobe.com/xap/1.0/');
xml.UpdateAttrAt('rdf:RDF|rdf:Description',True,'xmlns:dc','http://purl.org/dc/elements/1.1/');
xml.UpdateAttrAt('rdf:RDF|rdf:Description',True,'xmlns:xmpMM','http://ns.adobe.com/xap/1.0/mm/');
xml.UpdateAttrAt('rdf:RDF|rdf:Description',True,'xmlns:pdf','http://ns.adobe.com/pdf/1.3/');
xml.UpdateAttrAt('rdf:RDF|rdf:Description',True,'xmlns:xmpRights','http://ns.adobe.com/xap/1.0/rights/');
dt := TCkDateTime.Create;
dt.SetFromCurrentSystemTime();
ts := dt.GetAsTimestamp(True);
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 := TStringBuilder.Create;
sbDocId.Append('uuid:');
sbDocId.AppendUuid(True);
xml.UpdateChildContent('rdf:RDF|rdf:Description|xmpMM:DocumentID',sbDocId.GetAsString());
sbInstanceId := TStringBuilder.Create;
sbInstanceId.Append('uuid:');
sbInstanceId.AppendUuid(True);
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',True,'xmlns:zf','urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#');
xml.UpdateAttrAt('rdf:RDF|rdf:Description',True,'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 := TStringBuilder.Create;
xml.GetXmlSb(sb);
success := pdf.UpdateMetadata(sb,'c:/temp/qa_output/out.pdf');
if (success = True) then
begin
WriteLn('Success');
end
else
begin
WriteLn(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.
WriteLn('OK');
pdf.Free;
xml.Free;
sbExisting.Free;
dt.Free;
sbDocId.Free;
sbInstanceId.Free;
sb.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.