Objective-C
Objective-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 Objective-C Downloads
#import <CkoPdf.h>
#import <CkoXml.h>
#import <CkoStringBuilder.h>
#import <CkoDateTime.h>
#import <NSString.h>
BOOL success = NO;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoPdf *pdf = [[CkoPdf alloc] init];
// Load a PDF file.
// If the PDF file already has metadata, we'll update it.
// Otherwise this example adds the metadata.
success = [pdf LoadFile: @"qa_data/pdf/blank_with_metadata.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
CkoXml *xml = [[CkoXml alloc] init];
CkoStringBuilder *sbExisting = [[CkoStringBuilder alloc] init];
BOOL hasMetadata = [pdf GetMetadata: sbExisting];
if (hasMetadata == YES) {
[xml LoadSb: sbExisting autoTrim: YES];
}
else {
// Otherwise create the bare minimum XMP metadata.
xml.Tag = @"x:xmpmeta";
[xml AddAttribute: @"xmlns:x" value: @"adobe:ns:meta/"];
[xml AddAttribute: @"x:xmptk" value: @"Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 "];
[xml UpdateAttrAt: @"rdf:RDF" autoCreate: YES attrName: @"xmlns:rdf" attrValue: @"http://www.w3.org/1999/02/22-rdf-syntax-ns#"];
[xml UpdateAttrAt: @"rdf:RDF|rdf:Description" autoCreate: YES attrName: @"rdf:about" attrValue: @""];
[xml UpdateAttrAt: @"rdf:RDF|rdf:Description" autoCreate: YES attrName: @"xmlns:xmp" attrValue: @"http://ns.adobe.com/xap/1.0/"];
[xml UpdateAttrAt: @"rdf:RDF|rdf:Description" autoCreate: YES attrName: @"xmlns:dc" attrValue: @"http://purl.org/dc/elements/1.1/"];
[xml UpdateAttrAt: @"rdf:RDF|rdf:Description" autoCreate: YES attrName: @"xmlns:xmpMM" attrValue: @"http://ns.adobe.com/xap/1.0/mm/"];
[xml UpdateAttrAt: @"rdf:RDF|rdf:Description" autoCreate: YES attrName: @"xmlns:pdf" attrValue: @"http://ns.adobe.com/pdf/1.3/"];
[xml UpdateAttrAt: @"rdf:RDF|rdf:Description" autoCreate: YES attrName: @"xmlns:xmpRights" attrValue: @"http://ns.adobe.com/xap/1.0/rights/"];
CkoDateTime *dt = [[CkoDateTime alloc] init];
[dt SetFromCurrentSystemTime];
NSString *ts = [dt GetAsTimestamp: YES];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|xmp:ModifyDate" value: ts];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|xmp:CreateDate" value: ts];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|xmp:MetadataDate" value: ts];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|xmp:CreatorTool" value: @"My Custom Application"];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|dc:format" value: @"application/pdf"];
CkoStringBuilder *sbDocId = [[CkoStringBuilder alloc] init];
[sbDocId Append: @"uuid:"];
[sbDocId AppendUuid: YES];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|xmpMM:DocumentID" value: [sbDocId GetAsString]];
CkoStringBuilder *sbInstanceId = [[CkoStringBuilder alloc] init];
[sbInstanceId Append: @"uuid:"];
[sbInstanceId AppendUuid: YES];
[xml UpdateChildContent: @"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: @"rdf:RDF|rdf:Description" autoCreate: YES attrName: @"xmlns:zf" attrValue: @"urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#"];
[xml UpdateAttrAt: @"rdf:RDF|rdf:Description" autoCreate: YES attrName: @"rdf:about" attrValue: @" "];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|zf:ConformanceLevel" value: @"BASIC"];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|zf:DocumentFileName" value: @"ZZZZZZ-invoice.xml"];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|zf:DocumentType" value: @"INVOICE"];
[xml UpdateChildContent: @"rdf:RDF|rdf:Description|zf:Version" value: @"1.0"];
// Create a new PDF with the updated metadata.
CkoStringBuilder *sb = [[CkoStringBuilder alloc] init];
[xml GetXmlSb: sb];
success = [pdf UpdateMetadata: sb outFilePath: @"c:/temp/qa_output/out.pdf"];
if (success == YES) {
NSLog(@"%@",@"Success");
}
else {
NSLog(@"%@",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.
NSLog(@"%@",@"OK");