objective-c - iOS 中的 PDF 包

标签 objective-c ios pdf core-graphics pdf-parsing

我一直在尝试提取 PDF 包中包含的 pdf 文档,但没有成功。我在任何地方都找不到文档或示例代码,但我知道这并非不可能,因为 Adob​​e Reader 应用程序和 PDFExpert 应用程序支持它。有可能他们有自己的解析器,我希望不会变成那样......

任何能为我指明正确方向的提示都将不胜感激

编辑:很长一段时间后,我重新开始研究这个问题,并终于弄明白了。 特别感谢 iPDFDev 为我指明了正确的方向!!

这是关于如何获取每个内部 CGPDFDocumentRef 的代码:

NSURL *url = [NSURL fileURLWithPath:filePath isDirectory:NO];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);
CGPDFDictionaryRef catalog = CGPDFDocumentGetCatalog(pdf);

CGPDFDictionaryRef names = NULL;
if (CGPDFDictionaryGetDictionary(catalog, "Names", &names)) {
    CGPDFDictionaryRef embFiles = NULL;
    if (CGPDFDictionaryGetDictionary(names, "EmbeddedFiles", &embFiles)) {
        // At this point you know this is a Package/Portfolio

        CGPDFArrayRef nameArray = NULL;
        CGPDFDictionaryGetArray(embFiles, "Names", &nameArray);

        // nameArray contains the inner documents
        // it brings the name and then a dictionary from where you can extract the pdf

        for (int i = 0; i < CGPDFArrayGetCount(nameArray); i+=2) {
            CGPDFStringRef name = NULL;
            CGPDFDictionaryRef dict = NULL;

            if (CGPDFArrayGetString(nameArray, i, &name) &&
                CGPDFArrayGetDictionary(nameArray, i+1, &dict)) {
                NSString *_name = [self convertPDFString:name];

                CGPDFDictionaryRef EF;
                if (CGPDFDictionaryGetDictionary(dict, "EF", &EF)) {
                    CGPDFStreamRef F;
                    if (CGPDFDictionaryGetStream(EF, "F", &F)) {
                        CFDataRef data = CGPDFStreamCopyData(F, NULL);
                        CGDataProviderRef provider = CGDataProviderCreateWithCFData(data);

                        CGPDFDocumentRef _doc = CGPDFDocumentCreateWithProvider(provider);
                        if (_doc) {
                            // save the docRef somewhere (_doc)
                            // save the pdf name somewhere (_name)
                        }

                        CFRelease(data);
                        CGDataProviderRelease(provider);
                    }
                }
            }
        }
    }
}



- (NSString *)convertPDFString:(CGPDFStringRef)string {
    CFStringRef cfString = CGPDFStringCopyTextString(string);
    NSString *result = [[NSString alloc] initWithString:(__bridge NSString *)cfString];
    CFRelease(cfString);
    return result;
}

最佳答案

对于 PDF 包,我假设您指的是 PDF 投资组合。 PDF 组合中的文件基本上是具有一些扩展属性的文档附件,它们位于 EmbeddedFiles 树中。您从文档目录字典开始。从文档目录字典中检索/Names 字典。从/Names 字典中,如果存在(可选),您可以检索/EmbeddedFiles 字典。如果它存在,它代表嵌入文件树的头部(PDF 规范中的名称树)。
PDF 规范(可在此处获得:http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/PDF32000_2008.pdf)在第 7.9.6 节中描述了名称树和您'您将了解如何解析树。
树将字符串标识符映射到文件规范字典(第 7.11.3 节)。从文件规范字典中,您可以检索作为嵌入文件流的/EF 键的值(第 7.11.4 节)。与此对象关联的流就是您要查找的文件内容。

关于objective-c - iOS 中的 PDF 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10196723/

相关文章:

ios - 在 iOS AddressBook 中使用未声明的标识符 'kABNicknameProperty' - 为什么?

javascript 多重下载 pdf ie

objective-c - 使用 objective-c 创建 HTTP POST 消息并处理返回的 XML 数据

iphone - UIImageView 动画在第一次运行时滞后

objective-c - Xcode 7 中的 DDHotKey

objective-c - Facebook SDK API IOS 发布到用户墙上时出错

ios - 如何在 View Controller 中多次加载相同的 View

java - 使用 itext 从 html 生成 pdf 时不采用 CSS 样式

javascript - 是否可以使用 JavaScript 在 PDF 中创建命名目标?

objective-c - 单个 View 中的两个 NSTableView