【问题标题】:iOS: Sample Code or Examples for CGPDFContextAddDocumentMetadataiOS:CGPDFContextAddDocumentMetadata 的示例代码或示例
【发布时间】:2012-07-03 03:44:19
【问题描述】:

我需要使用 Apple 的 Objective C SDK 将一些自定义元数据嵌入到 PDF 文件中。是否有任何可用的示例代码可以做到这一点?

我需要的是非常基本的。打开现有的 pdf 并添加一些元数据,然后保存。

存储的信息示例如下:客户姓名、联系电话、订购的商品。

我看到这个“CGPDFContextAddDocumentMetadata”支持元数据,但我不知道元数据是什么样的,以及它是如何传递给这个函数的。

任何帮助将不胜感激...:)

【问题讨论】:

    标签: ios xcode pdf metadata


    【解决方案1】:

    元数据是元数据(当然,Adobe 建议您使用 XMP XML)。只要您创建一个有效的 CFDataRef 并将其传递给 arg 2,您就可以很好地处理任何事情。例如,以下是如何将字符串“Hello World”传递到 PDF 的元数据中:

    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);    
    }
    

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

    【讨论】:

    • 感谢您的帮助!! :) 我的 PDF 当前由 UIGraphicsBeginPDFContextToFile 创建,生成的 PDF 为“firstDoc.pdf”。我怎样才能使它可以传递这个文件并向它添加元数据?您的代码创建了一个新的图形上下文并使用它来生成 pdf。
    • 当您生成上下文时,只需使用从字符串到元数据添加调用的所有内容并传入您的上下文。另外,不要忘记点赞和接受!
    • 我的问题是我的 PDF 文档中有多个页面。我不确定您的方法,但最后一次生成 PDF 时,我无法继续输入字符串。当我估计页面空间不足时,我必须调用一些“创建新页面功能”。
    • 元数据适用于 文档 而不是页面,因此调用一次即可终身使用。
    • 知道了。会试一试。顺便说一句...如何从 pdf 中检索“hello world”字符串和 XMP 内容?我可以通过在 Adob​​e Reader 上查看 PDF 文档的属性来查看它们吗?
    【解决方案2】:

    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();
    

    【讨论】:

    • 嗨科林,你知道如何读取添加的元数据吗?我尝试使用 CGPDFDocumentGetInfo -> CGPDFDictionaryGetObject 方法,但没有结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2011-12-17
    • 2016-12-01
    相关资源
    最近更新 更多