Unicode C
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
#include <C_CkPdfW.h>
#include <C_CkXmlW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkDateTimeW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkXmlW xml;
HCkStringBuilderW sbExisting;
BOOL hasMetadata;
HCkDateTimeW dt;
const wchar_t *ts;
HCkStringBuilderW sbDocId;
HCkStringBuilderW sbInstanceId;
HCkStringBuilderW sb;
success = FALSE;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
pdf = CkPdfW_Create();
// Load a PDF file.
// If the PDF file already has metadata, we'll update it.
// Otherwise this example adds the metadata.
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/blank_with_metadata.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
xml = CkXmlW_Create();
sbExisting = CkStringBuilderW_Create();
hasMetadata = CkPdfW_GetMetadata(pdf,sbExisting);
if (hasMetadata == TRUE) {
CkXmlW_LoadSb(xml,sbExisting,TRUE);
}
else {
// Otherwise create the bare minimum XMP metadata.
CkXmlW_putTag(xml,L"x:xmpmeta");
CkXmlW_AddAttribute(xml,L"xmlns:x",L"adobe:ns:meta/");
CkXmlW_AddAttribute(xml,L"x:xmptk",L"Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 ");
CkXmlW_UpdateAttrAt(xml,L"rdf:RDF",TRUE,L"xmlns:rdf",L"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
CkXmlW_UpdateAttrAt(xml,L"rdf:RDF|rdf:Description",TRUE,L"rdf:about",L"");
CkXmlW_UpdateAttrAt(xml,L"rdf:RDF|rdf:Description",TRUE,L"xmlns:xmp",L"http://ns.adobe.com/xap/1.0/");
CkXmlW_UpdateAttrAt(xml,L"rdf:RDF|rdf:Description",TRUE,L"xmlns:dc",L"http://purl.org/dc/elements/1.1/");
CkXmlW_UpdateAttrAt(xml,L"rdf:RDF|rdf:Description",TRUE,L"xmlns:xmpMM",L"http://ns.adobe.com/xap/1.0/mm/");
CkXmlW_UpdateAttrAt(xml,L"rdf:RDF|rdf:Description",TRUE,L"xmlns:pdf",L"http://ns.adobe.com/pdf/1.3/");
CkXmlW_UpdateAttrAt(xml,L"rdf:RDF|rdf:Description",TRUE,L"xmlns:xmpRights",L"http://ns.adobe.com/xap/1.0/rights/");
dt = CkDateTimeW_Create();
CkDateTimeW_SetFromCurrentSystemTime(dt);
ts = CkDateTimeW_getAsTimestamp(dt,TRUE);
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|xmp:ModifyDate",ts);
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|xmp:CreateDate",ts);
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|xmp:MetadataDate",ts);
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|xmp:CreatorTool",L"My Custom Application");
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|dc:format",L"application/pdf");
sbDocId = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbDocId,L"uuid:");
CkStringBuilderW_AppendUuid(sbDocId,TRUE);
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|xmpMM:DocumentID",CkStringBuilderW_getAsString(sbDocId));
sbInstanceId = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbInstanceId,L"uuid:");
CkStringBuilderW_AppendUuid(sbInstanceId,TRUE);
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|xmpMM:InstanceID",CkStringBuilderW_getAsString(sbInstanceId));
}
// Add our custom metadata tags to either the existing XML metdata, or the newly created metadata.
CkXmlW_UpdateAttrAt(xml,L"rdf:RDF|rdf:Description",TRUE,L"xmlns:zf",L"urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#");
CkXmlW_UpdateAttrAt(xml,L"rdf:RDF|rdf:Description",TRUE,L"rdf:about",L" ");
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|zf:ConformanceLevel",L"BASIC");
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|zf:DocumentFileName",L"ZZZZZZ-invoice.xml");
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|zf:DocumentType",L"INVOICE");
CkXmlW_UpdateChildContent(xml,L"rdf:RDF|rdf:Description|zf:Version",L"1.0");
// Create a new PDF with the updated metadata.
sb = CkStringBuilderW_Create();
CkXmlW_GetXmlSb(xml,sb);
success = CkPdfW_UpdateMetadata(pdf,sb,L"c:/temp/qa_output/out.pdf");
if (success == TRUE) {
wprintf(L"Success\n");
}
else {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
}
// 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");
CkPdfW_Dispose(pdf);
CkXmlW_Dispose(xml);
CkStringBuilderW_Dispose(sbExisting);
CkDateTimeW_Dispose(dt);
CkStringBuilderW_Dispose(sbDocId);
CkStringBuilderW_Dispose(sbInstanceId);
CkStringBuilderW_Dispose(sb);
}