Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkXml.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkPdf.pb"
IncludeFile "CkDateTime.pb"

Procedure ChilkatExample()

    success.i = 0

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

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Load a PDF file.
    ; If the PDF file already has metadata, we'll update it.
    ; Otherwise this example adds the metadata.
    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/blank_with_metadata.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sbExisting.i = CkStringBuilder::ckCreate()
    If sbExisting.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    hasMetadata.i = CkPdf::ckGetMetadata(pdf,sbExisting)
    If hasMetadata = 1
        CkXml::ckLoadSb(xml,sbExisting,1)
    Else

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

        dt.i = CkDateTime::ckCreate()
        If dt.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkDateTime::ckSetFromCurrentSystemTime(dt)
        ts.s = CkDateTime::ckGetAsTimestamp(dt,1)

        CkXml::ckUpdateChildContent(xml,"rdf:RDF|rdf:Description|xmp:ModifyDate",ts)
        CkXml::ckUpdateChildContent(xml,"rdf:RDF|rdf:Description|xmp:CreateDate",ts)
        CkXml::ckUpdateChildContent(xml,"rdf:RDF|rdf:Description|xmp:MetadataDate",ts)

        CkXml::ckUpdateChildContent(xml,"rdf:RDF|rdf:Description|xmp:CreatorTool","My Custom Application")
        CkXml::ckUpdateChildContent(xml,"rdf:RDF|rdf:Description|dc:format","application/pdf")

        sbDocId.i = CkStringBuilder::ckCreate()
        If sbDocId.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkStringBuilder::ckAppend(sbDocId,"uuid:")
        CkStringBuilder::ckAppendUuid(sbDocId,1)
        CkXml::ckUpdateChildContent(xml,"rdf:RDF|rdf:Description|xmpMM:DocumentID",CkStringBuilder::ckGetAsString(sbDocId))

        sbInstanceId.i = CkStringBuilder::ckCreate()
        If sbInstanceId.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkStringBuilder::ckAppend(sbInstanceId,"uuid:")
        CkStringBuilder::ckAppendUuid(sbInstanceId,1)
        CkXml::ckUpdateChildContent(xml,"rdf:RDF|rdf:Description|xmpMM:InstanceID",CkStringBuilder::ckGetAsString(sbInstanceId))

    EndIf

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

    ; Create a new PDF with the updated metadata.
    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::ckGetXmlSb(xml,sb)
    success = CkPdf::ckUpdateMetadata(pdf,sb,"c:/temp/qa_output/out.pdf")
    If success = 1
        Debug "Success"
    Else
        Debug CkPdf::ckLastErrorText(pdf)
    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.

    Debug "OK"


    CkPdf::ckDispose(pdf)
    CkXml::ckDispose(xml)
    CkStringBuilder::ckDispose(sbExisting)
    CkDateTime::ckDispose(dt)
    CkStringBuilder::ckDispose(sbDocId)
    CkStringBuilder::ckDispose(sbInstanceId)
    CkStringBuilder::ckDispose(sb)


    ProcedureReturn
EndProcedure