如何在iOS上读取-修改-写入PDF(CGPDFDocument)? [英] How can I read-modify-write a PDF (CGPDFDocument) on iOS?

查看:500
本文介绍了如何在iOS上读取-修改-写入PDF(CGPDFDocument)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改iOS上现有PDF的PDF元数据(标题,作者等). 虽然很容易找到用于PDF解析和从头开始创建PDF的示例代码,但是似乎没有一种简单的方法可以将现有的PDF转储到新文件中,并稍加修改即可.

I am trying to modify the PDF metadata (title, author, etc.) of an existing PDF on iOS. While it is easy to find sample code for PDF parsing and for PDF creation from scratch, there does not seem to be an easy way to dump an existing PDF into a new file - and modify it just slightly.

更具体地说,如何大致读取如下所示的PDF时获得的信息:

More specifically, how do I get the info obtained when reading a PDF roughly like this:

CGPDFDocumentRef myPDF = CGPDFDocumentCreateWithURL((CFURLRef)urlOfInputPDF);
CGPDFDictionaryRef myPDFDict=CGPDFDocumentGetInfo(myPDF);

进入新的PDF,我将大致以此方式创建:

into the new PDF, which I would create roughly this way:

CGRect pageRect;
CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0,
                                         &kCFTypeDictionaryKeyCallBacks,
                                         &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
pdfContext = CGPDFContextCreateWithURL (urlOfOutputPDF, &pageRect, myDictionary);

很明显,字典类型不匹配,并且以这种方式创建的输出与页面(而不是文档)相关.

Obviously, the dictionary types do not match, and the output created this way is page (not document) related.

任何想法,如何将整个CGPDFDocumentRef复制到一个新文件中?以及如何使其可变,以便我可以在保存之前修改元数据?

Any ideas, how I can copy the whole CGPDFDocumentRef into a new file? And how to make it mutable, so I can modify the meta data before saving?

推荐答案

我不知道使用CoreGraphics可以遍历每个页面并将它们打印到新文档中.至少那是我在PSPDFKit中所做的.对于较大的文档,重新渲染整个文档非常慢.

I'm not aware of a way with CoreGraphics than to iterate over each page and print them into the new document. At least that's what I'm doing in PSPDFKit. And re-rendering a whole document is quite slow for larger documents.

当您这样做时,您也会丢失一些元数据-也许直接对PDF进行更直接的操作是更好的方法-但请注意,如果您直接编辑字符串,则需要更新PDF中的外部参照预告片,因为预告片是字节坐标的映射,一旦添加/删除字符,该映射就会更改.

You also loose some metadata when you go that way - maybe a much more direct manipulation of the PDF is a better way - but note that you need to update the xref trailer in the PDF if you directly edit strings, since the trailer is a map of byte-coordinates that will change once you add/delete characters.

这篇关于如何在iOS上读取-修改-写入PDF(CGPDFDocument)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆