Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

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

$oPdf = ObjCreate("Chilkat.Pdf")

; Load a PDF file.
; If the PDF file already has metadata, we'll update it.
; Otherwise this example adds the metadata.
$bSuccess = $oPdf.LoadFile("qa_data/pdf/blank_with_metadata.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

$oXml = ObjCreate("Chilkat.Xml")
$oSbExisting = ObjCreate("Chilkat.StringBuilder")

Local $bHasMetadata = $oPdf.GetMetadata($oSbExisting)
If ($bHasMetadata = True) Then
    $oXml.LoadSb($oSbExisting,True)
Else

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

    $oDt = ObjCreate("Chilkat.CkDateTime")
    $oDt.SetFromCurrentSystemTime()
Local $sTs = $oDt.GetAsTimestamp(True)

    $oXml.UpdateChildContent "rdf:RDF|rdf:Description|xmp:ModifyDate",$sTs
    $oXml.UpdateChildContent "rdf:RDF|rdf:Description|xmp:CreateDate",$sTs
    $oXml.UpdateChildContent "rdf:RDF|rdf:Description|xmp:MetadataDate",$sTs

    $oXml.UpdateChildContent "rdf:RDF|rdf:Description|xmp:CreatorTool","My Custom Application"
    $oXml.UpdateChildContent "rdf:RDF|rdf:Description|dc:format","application/pdf"

    $oSbDocId = ObjCreate("Chilkat.StringBuilder")
    $oSbDocId.Append("uuid:")
    $oSbDocId.AppendUuid(True)
    $oXml.UpdateChildContent "rdf:RDF|rdf:Description|xmpMM:DocumentID",$oSbDocId.GetAsString()

    $oSbInstanceId = ObjCreate("Chilkat.StringBuilder")
    $oSbInstanceId.Append("uuid:")
    $oSbInstanceId.AppendUuid(True)
    $oXml.UpdateChildContent "rdf:RDF|rdf:Description|xmpMM:InstanceID",$oSbInstanceId.GetAsString()

EndIf

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

; Create a new PDF with the updated metadata.
$oSb = ObjCreate("Chilkat.StringBuilder")
$oXml.GetXmlSb($oSb)
$bSuccess = $oPdf.UpdateMetadata($oSb,"c:/temp/qa_output/out.pdf")
If ($bSuccess = True) Then
    ConsoleWrite("Success" & @CRLF)
Else
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
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.

ConsoleWrite("OK" & @CRLF)