Delphi DLL
Delphi DLL
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 DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, Pdf, Xml, CkDateTime;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
pdf: HCkPdf;
xml: HCkXml;
sbExisting: HCkStringBuilder;
hasMetadata: Boolean;
dt: HCkDateTime;
ts: PWideChar;
sbDocId: HCkStringBuilder;
sbInstanceId: HCkStringBuilder;
sb: HCkStringBuilder;
begin
success := False;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
pdf := CkPdf_Create();
// Load a PDF file.
// If the PDF file already has metadata, we'll update it.
// Otherwise this example adds the metadata.
success := CkPdf_LoadFile(pdf,'qa_data/pdf/blank_with_metadata.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
xml := CkXml_Create();
sbExisting := CkStringBuilder_Create();
hasMetadata := CkPdf_GetMetadata(pdf,sbExisting);
if (hasMetadata = True) then
begin
CkXml_LoadSb(xml,sbExisting,True);
end
else
begin
// Otherwise create the bare minimum XMP metadata.
CkXml_putTag(xml,'x:xmpmeta');
CkXml_AddAttribute(xml,'xmlns:x','adobe:ns:meta/');
CkXml_AddAttribute(xml,'x:xmptk','Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 ');
CkXml_UpdateAttrAt(xml,'rdf:RDF',True,'xmlns:rdf','http://www.w3.org/1999/02/22-rdf-syntax-ns#');
CkXml_UpdateAttrAt(xml,'rdf:RDF|rdf:Description',True,'rdf:about','');
CkXml_UpdateAttrAt(xml,'rdf:RDF|rdf:Description',True,'xmlns:xmp','http://ns.adobe.com/xap/1.0/');
CkXml_UpdateAttrAt(xml,'rdf:RDF|rdf:Description',True,'xmlns:dc','http://purl.org/dc/elements/1.1/');
CkXml_UpdateAttrAt(xml,'rdf:RDF|rdf:Description',True,'xmlns:xmpMM','http://ns.adobe.com/xap/1.0/mm/');
CkXml_UpdateAttrAt(xml,'rdf:RDF|rdf:Description',True,'xmlns:pdf','http://ns.adobe.com/pdf/1.3/');
CkXml_UpdateAttrAt(xml,'rdf:RDF|rdf:Description',True,'xmlns:xmpRights','http://ns.adobe.com/xap/1.0/rights/');
dt := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(dt);
ts := CkDateTime__getAsTimestamp(dt,True);
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|xmp:ModifyDate',ts);
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|xmp:CreateDate',ts);
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|xmp:MetadataDate',ts);
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|xmp:CreatorTool','My Custom Application');
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|dc:format','application/pdf');
sbDocId := CkStringBuilder_Create();
CkStringBuilder_Append(sbDocId,'uuid:');
CkStringBuilder_AppendUuid(sbDocId,True);
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|xmpMM:DocumentID',CkStringBuilder__getAsString(sbDocId));
sbInstanceId := CkStringBuilder_Create();
CkStringBuilder_Append(sbInstanceId,'uuid:');
CkStringBuilder_AppendUuid(sbInstanceId,True);
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|xmpMM:InstanceID',CkStringBuilder__getAsString(sbInstanceId));
end;
// Add our custom metadata tags to either the existing XML metdata, or the newly created metadata.
CkXml_UpdateAttrAt(xml,'rdf:RDF|rdf:Description',True,'xmlns:zf','urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#');
CkXml_UpdateAttrAt(xml,'rdf:RDF|rdf:Description',True,'rdf:about',' ');
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|zf:ConformanceLevel','BASIC');
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|zf:DocumentFileName','ZZZZZZ-invoice.xml');
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|zf:DocumentType','INVOICE');
CkXml_UpdateChildContent(xml,'rdf:RDF|rdf:Description|zf:Version','1.0');
// Create a new PDF with the updated metadata.
sb := CkStringBuilder_Create();
CkXml_GetXmlSb(xml,sb);
success := CkPdf_UpdateMetadata(pdf,sb,'c:/temp/qa_output/out.pdf');
if (success = True) then
begin
Memo1.Lines.Add('Success');
end
else
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
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');
CkPdf_Dispose(pdf);
CkXml_Dispose(xml);
CkStringBuilder_Dispose(sbExisting);
CkDateTime_Dispose(dt);
CkStringBuilder_Dispose(sbDocId);
CkStringBuilder_Dispose(sbInstanceId);
CkStringBuilder_Dispose(sb);
end;