我已经尝试了一段时间,以便能够提取包含在pdf包中的PDF文档,但没有成功。我在任何地方都没有找到文档或示例代码,但我知道这不是不可能的,因为Adobe 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;
}发布于 2012-04-18 19:14:22
通过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节)。与此对象关联的流就是您正在查找的文件内容。
https://stackoverflow.com/questions/10196723
复制相似问题