在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类似如下:





  application/pdf
  
    
      
    
  
  
    
      
    
  
  
    
      
    
  


  
  


  
  2011-02-10T11:41:05+02:00
  2011-02-10T11:41:06+02:00



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


} 


你可能感兴趣的:(Mac开发)