DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Handle hoXml
Variant vSbExisting
Handle hoSbExisting
Boolean iHasMetadata
Handle hoDt
String sTs
Handle hoSbDocId
Handle hoSbInstanceId
Variant vSb
Handle hoSb
String sTemp1
Move False To iSuccess
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
// Load a PDF file.
// If the PDF file already has metadata, we'll update it.
// Otherwise this example adds the metadata.
Get ComLoadFile Of hoPdf "qa_data/pdf/blank_with_metadata.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbExisting
If (Not(IsComObjectCreated(hoSbExisting))) Begin
Send CreateComObject of hoSbExisting
End
Get pvComObject of hoSbExisting to vSbExisting
Get ComGetMetadata Of hoPdf vSbExisting To iHasMetadata
If (iHasMetadata = True) Begin
Get pvComObject of hoSbExisting to vSbExisting
Get ComLoadSb Of hoXml vSbExisting True To iSuccess
End
Else Begin
// Otherwise create the bare minimum XMP metadata.
Set ComTag Of hoXml To "x:xmpmeta"
Get ComAddAttribute Of hoXml "xmlns:x" "adobe:ns:meta/" To iSuccess
Get ComAddAttribute Of hoXml "x:xmptk" "Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 " To iSuccess
Get ComUpdateAttrAt Of hoXml "rdf:RDF" True "xmlns:rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#" To iSuccess
Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "rdf:about" "" To iSuccess
Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:xmp" "http://ns.adobe.com/xap/1.0/" To iSuccess
Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:dc" "http://purl.org/dc/elements/1.1/" To iSuccess
Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:xmpMM" "http://ns.adobe.com/xap/1.0/mm/" To iSuccess
Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:pdf" "http://ns.adobe.com/pdf/1.3/" To iSuccess
Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:xmpRights" "http://ns.adobe.com/xap/1.0/rights/" To iSuccess
Get Create (RefClass(cComCkDateTime)) To hoDt
If (Not(IsComObjectCreated(hoDt))) Begin
Send CreateComObject of hoDt
End
Get ComSetFromCurrentSystemTime Of hoDt To iSuccess
Get ComGetAsTimestamp Of hoDt True To sTs
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmp:ModifyDate" sTs
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmp:CreateDate" sTs
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmp:MetadataDate" sTs
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmp:CreatorTool" "My Custom Application"
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|dc:format" "application/pdf"
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbDocId
If (Not(IsComObjectCreated(hoSbDocId))) Begin
Send CreateComObject of hoSbDocId
End
Get ComAppend Of hoSbDocId "uuid:" To iSuccess
Get ComAppendUuid Of hoSbDocId True To iSuccess
Get ComGetAsString Of hoSbDocId To sTemp1
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmpMM:DocumentID" sTemp1
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbInstanceId
If (Not(IsComObjectCreated(hoSbInstanceId))) Begin
Send CreateComObject of hoSbInstanceId
End
Get ComAppend Of hoSbInstanceId "uuid:" To iSuccess
Get ComAppendUuid Of hoSbInstanceId True To iSuccess
Get ComGetAsString Of hoSbInstanceId To sTemp1
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmpMM:InstanceID" sTemp1
End
// Add our custom metadata tags to either the existing XML metdata, or the newly created metadata.
Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:zf" "urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#" To iSuccess
Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "rdf:about" " " To iSuccess
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|zf:ConformanceLevel" "BASIC"
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|zf:DocumentFileName" "ZZZZZZ-invoice.xml"
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|zf:DocumentType" "INVOICE"
Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|zf:Version" "1.0"
// Create a new PDF with the updated metadata.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
If (Not(IsComObjectCreated(hoSb))) Begin
Send CreateComObject of hoSb
End
Get pvComObject of hoSb to vSb
Get ComGetXmlSb Of hoXml vSb To iSuccess
Get pvComObject of hoSb to vSb
Get ComUpdateMetadata Of hoPdf vSb "c:/temp/qa_output/out.pdf" To iSuccess
If (iSuccess = True) Begin
Showln "Success"
End
Else Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
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.
Showln "OK"
End_Procedure