Sample code for 30+ languages & platforms
Swift

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

Swift

func chilkatTest() {
    var success: Bool = false

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

    let pdf = CkoPdf()!

    // Load a PDF file.
    // If the PDF file already has metadata, we'll update it.
    // Otherwise this example adds the metadata.
    success = pdf.loadFile(filePath: "qa_data/pdf/blank_with_metadata.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    let xml = CkoXml()!
    let sbExisting = CkoStringBuilder()!

    var hasMetadata: Bool = pdf.getMetadata(sb: sbExisting)
    if hasMetadata == true {
        xml.loadSb(sb: sbExisting, autoTrim: true)
    }
    else {

        // Otherwise create the bare minimum XMP metadata.
        xml.tag = "x:xmpmeta"
        xml.addAttribute(name: "xmlns:x", value: "adobe:ns:meta/")
        xml.addAttribute(name: "x:xmptk", value: "Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 ")
        xml.updateAttrAt(tagPath: "rdf:RDF", autoCreate: true, attrName: "xmlns:rdf", attrValue: "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
        xml.updateAttrAt(tagPath: "rdf:RDF|rdf:Description", autoCreate: true, attrName: "rdf:about", attrValue: "")
        xml.updateAttrAt(tagPath: "rdf:RDF|rdf:Description", autoCreate: true, attrName: "xmlns:xmp", attrValue: "http://ns.adobe.com/xap/1.0/")
        xml.updateAttrAt(tagPath: "rdf:RDF|rdf:Description", autoCreate: true, attrName: "xmlns:dc", attrValue: "http://purl.org/dc/elements/1.1/")
        xml.updateAttrAt(tagPath: "rdf:RDF|rdf:Description", autoCreate: true, attrName: "xmlns:xmpMM", attrValue: "http://ns.adobe.com/xap/1.0/mm/")
        xml.updateAttrAt(tagPath: "rdf:RDF|rdf:Description", autoCreate: true, attrName: "xmlns:pdf", attrValue: "http://ns.adobe.com/pdf/1.3/")
        xml.updateAttrAt(tagPath: "rdf:RDF|rdf:Description", autoCreate: true, attrName: "xmlns:xmpRights", attrValue: "http://ns.adobe.com/xap/1.0/rights/")

        let dt = CkoDateTime()!
        dt.setFromCurrentSystemTime()
        var ts: String? = dt.get(asTimestamp: true)

        xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|xmp:ModifyDate", value: ts)
        xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|xmp:CreateDate", value: ts)
        xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|xmp:MetadataDate", value: ts)

        xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|xmp:CreatorTool", value: "My Custom Application")
        xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|dc:format", value: "application/pdf")

        let sbDocId = CkoStringBuilder()!
        sbDocId.append(value: "uuid:")
        sbDocId.appendUuid(lowerCase: true)
        xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|xmpMM:DocumentID", value: sbDocId.getAsString())

        let sbInstanceId = CkoStringBuilder()!
        sbInstanceId.append(value: "uuid:")
        sbInstanceId.appendUuid(lowerCase: true)
        xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|xmpMM:InstanceID", value: sbInstanceId.getAsString())

    }

    // Add our custom metadata tags to either the existing XML metdata, or the newly created metadata.
    xml.updateAttrAt(tagPath: "rdf:RDF|rdf:Description", autoCreate: true, attrName: "xmlns:zf", attrValue: "urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#")
    xml.updateAttrAt(tagPath: "rdf:RDF|rdf:Description", autoCreate: true, attrName: "rdf:about", attrValue: " ")
    xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|zf:ConformanceLevel", value: "BASIC")
    xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|zf:DocumentFileName", value: "ZZZZZZ-invoice.xml")
    xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|zf:DocumentType", value: "INVOICE")
    xml.updateChildContent(tagPath: "rdf:RDF|rdf:Description|zf:Version", value: "1.0")

    // Create a new PDF with the updated metadata.
    let sb = CkoStringBuilder()!
    xml.getSb(sb: sb)
    success = pdf.updateMetadata(sb: sb, outFilePath: "c:/temp/qa_output/out.pdf")
    if success == true {
        print("Success")
    }
    else {
        print("\(pdf.lastErrorText!)")
    }

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

    print("OK")

}