philip abraham
philip abraham

Reputation: 587

How to add the metadata to a pdf in Objective C?

How to add the metadata to a pdf in Objective C? I came across a class method called CGPDFContextAddDocumentMetadata, but could not find any documentation of how to achieve it.

Upvotes: 1

Views: 1670

Answers (1)

iPDFdev
iPDFdev

Reputation: 5834

The metadata that can be added with this method is XMP metadata. It is XML based and looks like this (it is fully documented here: http://www.adobe.com/products/xmp/):

<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c041">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <dc:format>application/pdf</dc:format>
  <dc:title>
    <rdf:Alt>
      <rdf:li />
    </rdf:Alt>
  </dc:title>
  <dc:description>
    <rdf:Alt>
      <rdf:li />
    </rdf:Alt>
  </dc:description>
  <dc:creator>
    <rdf:Seq>
      <rdf:li />
    </rdf:Seq>
  </dc:creator>
</rdf:Description>
<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
  <pdf:Keywords />
  <pdf:Producer />
</rdf:Description>
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
  <xmp:CreatorTool />
  <xmp:CreateDate>2011-02-10T11:41:05+02:00</xmp:CreateDate>
  <xmp:ModifyDate>2011-02-10T11:41:06+02:00</xmp:ModifyDate>
</rdf:Description></rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>

The method looks like this:

void CGPDFContextAddDocumentMetadata(CGContextRef context,CFDataRef metadata);

'context' is your pdf context, metadata is your XMP metadata. You can construct the metadata in a NSString object (xmpString variable below) and then convert the NSString to CFDataRef:

CFDataRef xmpDataRef = (CFDataRef)[xmpString dataUsingEncoding:NSUTF8StringEncoding];

Upvotes: 2

Related Questions