Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

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

loPdf = createobject("CkPdf")

// Load a PDF file.
// If the PDF file already has metadata, we'll update it.
// Otherwise this example adds the metadata.
llSuccess = loPdf.LoadFile("qa_data/pdf/blank_with_metadata.pdf")
if (llSuccess = .F.) then
    ? loPdf.LastErrorText
    release loPdf
    return
endif

loXml = createobject("CkXml")
loSbExisting = createobject("CkStringBuilder")

llHasMetadata = loPdf.GetMetadata(loSbExisting)
if (llHasMetadata = .T.) then
    loXml.LoadSb(loSbExisting,.T.)
else

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

    loDt = createobject("CkDateTime")
    loDt.SetFromCurrentSystemTime()
    lcTs = loDt.GetAsTimestamp(.T.)

    loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:ModifyDate",lcTs)
    loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:CreateDate",lcTs)
    loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:MetadataDate",lcTs)

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

    loSbDocId = createobject("CkStringBuilder")
    loSbDocId.Append("uuid:")
    loSbDocId.AppendUuid(.T.)
    loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmpMM:DocumentID",loSbDocId.GetAsString())

    loSbInstanceId = createobject("CkStringBuilder")
    loSbInstanceId.Append("uuid:")
    loSbInstanceId.AppendUuid(.T.)
    loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmpMM:InstanceID",loSbInstanceId.GetAsString())

endif

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

// Create a new PDF with the updated metadata.
loSb = createobject("CkStringBuilder")
loXml.GetXmlSb(loSb)
llSuccess = loPdf.UpdateMetadata(loSb,"c:/temp/qa_output/out.pdf")
if (llSuccess = .T.) then
    ? "Success"
else
    ? loPdf.LastErrorText
endif

// 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.

? "OK"


release loPdf
release loXml
release loSbExisting
release loDt
release loSbDocId
release loSbInstanceId
release loSb