Question

I'm working on a project and I need to create and set my own metadata on a PDF (in order to set a GUID directly into the file).

I am currently able to set classic metadata (e.g creator, keywords...etc) but I can't figure out how to add a custom field.

Here's how I set the metadata:

CFMutableDictionaryRef auxInfo = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, NULL, NULL);
CFDictionaryAddValue(auxInfo, kCGPDFContextCreator, CFSTR("John doo"));
CFDictionaryAddValue(auxInfo, kCGPDFContextAuthor, CFSTR("foo bar"));
CFDictionaryRef auxillaryInformation = CFDictionaryCreateCopy(kCFAllocatorDefault, auxInfo);
CFRelease(auxInfo);

// create a context to draw into
CGContextRef graphicContext = CGPDFContextCreate(PDFDataConsumer, &mediaRect, auxillaryInformation);
CFRelease(auxillaryInformation);
CGDataConsumerRelease(PDFDataConsumer);

I tried to replace the kCGPDFContextThing by a custom name but then when I read the metadata, it doesn't appear at all.

For reading the meta I used that

CGPDFDictionaryRef dict = CGPDFDocumentGetInfo(*pdfDoc);
CGPDFDictionaryGetString(dict, "Creator", &objectValue);

I also try that to add a meta :

NSString* str= @"Hello World";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
CFDataRef cfdata = CFDataCreate(NULL, [data bytes], [data length]);

CGPDFContextAddDocumentMetadata(graphicContext, cfdata);

But it doesn't seems to work neither, and I'm not sure I understand correctly what's done there.

I am also using PDFNet SDK to help the editing, but it doesn't seems to provide any help about metadata so I'm using quartz.

Any help or advice or anything would be welcome, quite lost and I am not an iOS expert at all !!!

Was it helpful?

Solution

You can do low-level editing of a PDF in PDFNet. Briefly:

Create a new custom entry (on the document's root):

Obj* cust_dict = [[myPDFDoc GetRoot] PutDict:@"_MyCustomData" ];
[cust_dict PutText:@"_myGUID" value:@"123-4567-890"];

Read the custom entry:

Obj* dict = [[myPDFDoc GetRoot] FindObj:@"_MyCustomData"];
DictIterator* itr = [dict Get:@"_myGUID"];
Obj* strObj = [itr Value];
NSString* str = [strObj GetAsPDFText];
NSLog(@"guid is %@", str);

Delete a custom entry:

[[myPDFDoc GetRoot] EraseDictElementWithKey:@"_MyCustomData"];

You could also place metadata on a page's root, or in a more standard location, as outlined here: https://groups.google.com/d/msg/pdfnet-sdk/gtPjLZVbRSQ/Tv5DTb9pRXkJ

For more specific information about adding XMP metadata (as your tag implies you're interested in), try searching XMP on the PDFNet support forum: https://groups.google.com/forum/#!searchin/pdfnet-sdk/XMP

I'm not sure how you would do this with Quartz.

Disclosure: I work for PDFTron, maker of PDFNet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top