首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS: CGPDFContextAddDocumentMetadata的示例代码或示例

iOS: CGPDFContextAddDocumentMetadata的示例代码或示例
EN

Stack Overflow用户
提问于 2012-07-03 11:44:19
回答 2查看 2.9K关注 0票数 2

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

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

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

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

我们将非常感谢您的帮助...:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-03 11:48:27

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

代码语言:javascript
复制
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文件可能如下所示:

代码语言:javascript
复制
<?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"?>
票数 1
EN

Stack Overflow用户

发布于 2014-03-28 22:43:26

UIKit有一些方法可以让这一切变得更简单:

代码语言:javascript
复制
// 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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11304166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档