在OS X下编辑提取PDF metadata

http://stackoverflow.com/questions/11304166/ios-sample-code-or-examples-for-cgpdfcontextadddocumentmetadata

Metadata is metadata (of course, it is recommended by Adobe that you use XMP XML). So long as you create a valid CFDataRef and pass it into arg two, you're pretty much good to go with anything. For example, here's how to pass the string "Hello World" into a PDF's metadata:

void MakeAPDF()
{

    CGRect mediaRect = CGRectMake(0, 0, 400, 600);
    // use your own rect instead

    CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
    CGDataConsumerRef PDFDataConsumer = CGDataConsumerCreateWithCFData(result);

    // mark the PDF as coming from your program
    CFMutableDictionaryRef auxInfo = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, NULL, NULL);
    CFDictionaryAddValue(auxInfo, kCGPDFContextCreator, CFSTR("Your Programs Name"));
    CFDictionaryRef auxillaryInformation = CFDictionaryCreateCopy(kCFAllocatorDefault, auxInfo);
    CFRelease(auxInfo);

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

    // actually make the call to embed your String
    NSString* str= @"Hello World";
    NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
    CFDataRef cfdata = CFDataCreate(NULL, [data bytes], [data length]);

    CGPDFContextAddDocumentMetadata(graphicContext, cfdata);

    CGContextBeginPage(graphicContext, &mediaRect);
    // do your drawing, like this grey rectangle
    CGContextSetGrayFillColor(graphicContext, 0.5, 0.5);
    CGContextAddRect(graphicContext, mediaRect);
    CGContextFillPath(graphicContext);
    // end your drawing

    CGContextEndPage(graphicContext);
    CGContextFlush(graphicContext);
    CGPDFContextClose(graphicContext);    
}

一个xml类似如下:

<?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"?>

UIKit也能做一些:

// use your own rect instead
CGRect pageRect = CGRectMake(0, 0, 400, 600);

// mark the PDF as coming from your program
NSDictionary *auxillaryInformation = @{(NSString *)kCGPDFContextCreator: @"Koedal, Inc."};
UIGraphicsBeginPDFContextToFile([[self URLOfPDF] path], pageRect, auxillaryInformation);


// Embed metadata into PDF, I'm pulling it from a textView
NSString *PDFMetadata = self.metadataTextView.text;
NSData *data = [PDFMetadata  dataUsingEncoding:NSUTF8StringEncoding];

CGContextRef PDFContext = UIGraphicsGetCurrentContext();
CGPDFContextAddDocumentMetadata(PDFContext, (CFDataRef)data);

NSDictionary *textAttributes = @{ NSFontAttributeName : [UIFont boldSystemFontOfSize:20],
                                  NSForegroundColorAttributeName : [UIColor blackColor]
                                  };

// do your drawing, like drawing this string
UIGraphicsBeginPDFPage();
NSString *content = self.contentTextField.text;
[content drawAtPoint:CGPointMake(150, 280) withAttributes:textAttributes];

// end your drawing
UIGraphicsEndPDFContext();

又或者我们要提取xml:

http://www.sprinkleofcocoa.com/2010/09/finally-apple-embraces-standard-for.html

CFDataRef ExtractMetaDataFromPDFData(CFDataRef pdf)
{


    CFDataRef result = 0;

    CFRetain(pdf);
    const UInt8 * pdfData = CFDataGetBytePtr(pdf);
    CFIndex pdfDataLength = CFDataGetLength(pdf);
    CGDataProviderRef dataProvider = CGDataProviderCreateWithData(kCFAllocatorDefault, pdfData, pdfDataLength, NULL);
    CGPDFDocumentRef pdfDocument = CGPDFDocumentCreateWithProvider(dataProvider);
    CGDataProviderRelease(dataProvider);

    if(pdfDocument)
    {
    CGPDFDictionaryRef docDict = CGPDFDocumentGetCatalog(pdfDocument);
    CGPDFStreamRef metastream = 0;
    if(CGPDFDictionaryGetStream(docDict,"Metadata", &metastream))
    {
    CGPDFDataFormat format = CGPDFDataFormatRaw;
    CFDataRef streamData = CGPDFStreamCopyData(metastream, &format);
    if(streamData)
    {
    if(format == CGPDFDataFormatRaw)
    {
    result = streamData;
    CFRetain(result);
    }
    }
    }
    CGPDFDocumentRelease(pdfDocument);
    }
    CFRelease(pdf);

    return result; // check to see if this is your XML
    //remember to release result when done


} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值