Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkPdfW.h>
#include <CkXmlW.h>
#include <CkStringBuilderW.h>
#include <CkDateTimeW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkPdfW pdf;

    // Load a PDF file.
    // If the PDF file already has metadata, we'll update it.
    // Otherwise this example adds the metadata.
    success = pdf.LoadFile(L"qa_data/pdf/blank_with_metadata.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    CkXmlW xml;
    CkStringBuilderW sbExisting;

    bool hasMetadata = pdf.GetMetadata(sbExisting);
    if (hasMetadata == true) {
        xml.LoadSb(sbExisting,true);
    }
    else {

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

        CkDateTimeW dt;
        dt.SetFromCurrentSystemTime();
        const wchar_t *ts = dt.getAsTimestamp(true);

        xml.UpdateChildContent(L"rdf:RDF|rdf:Description|xmp:ModifyDate",ts);
        xml.UpdateChildContent(L"rdf:RDF|rdf:Description|xmp:CreateDate",ts);
        xml.UpdateChildContent(L"rdf:RDF|rdf:Description|xmp:MetadataDate",ts);

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

        CkStringBuilderW sbDocId;
        sbDocId.Append(L"uuid:");
        sbDocId.AppendUuid(true);
        xml.UpdateChildContent(L"rdf:RDF|rdf:Description|xmpMM:DocumentID",sbDocId.getAsString());

        CkStringBuilderW sbInstanceId;
        sbInstanceId.Append(L"uuid:");
        sbInstanceId.AppendUuid(true);
        xml.UpdateChildContent(L"rdf:RDF|rdf:Description|xmpMM:InstanceID",sbInstanceId.getAsString());

    }

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

    // Create a new PDF with the updated metadata.
    CkStringBuilderW sb;
    xml.GetXmlSb(sb);
    success = pdf.UpdateMetadata(sb,L"c:/temp/qa_output/out.pdf");
    if (success == true) {
        wprintf(L"Success\n");
    }
    else {
        wprintf(L"%s\n",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.

    wprintf(L"OK\n");
    }