由于 PDFKit 在 iOS 上不可用,如何在该环境中获取 pdf 文档的大纲? FastPdfKit 或 PSPDFKit 等商业图书馆是唯一的解决方案吗?
最佳答案
访问 pdf 大纲并不难。我的outline parser有大约 420 LOC。我会发布一些片段,所以你会明白的。我无法发布完整代码,因为它是一个商业图书馆。
你基本上是这样开始的:
CGPDFDictionaryRef outlineRef;
if(CGPDFDictionaryGetDictionary(pdfDocDictionary, "Outlines", &outlineRef)) {
继续
NSArray *outlineElements = nil;
CGPDFDictionaryRef firstEntry;
if (CGPDFDictionaryGetDictionary(outlineRef, "First", &firstEntry)) {
NSMutableArray *pageCache = [NSMutableArray arrayWithCapacity:CGPDFDocumentGetNumberOfPages(documentRef)];
outlineElements = [self parseOutlineElements:firstEntry level:0 error:&error documentRef:documentRef cache:pageCache];
}else {
PSPDFLogWarning(@"Error while parsing outline. First entry not found!");
}
你像这样解析单个项目:
// parse title
NSString *outlineTitle = stringFromCGPDFDictionary(outlineElementRef, @"Title");
PSPDFLogVerbose(@"outline title: %@", outlineTitle);
if (!outlineTitle) {
if (error_) {
*error_ = [NSError errorWithDomain:kPSPDFOutlineParserErrorDomain code:1 userInfo:nil];
}
return nil;
}
NSString *namedDestination = nil;
CGPDFObjectRef destinationRef;
if (CGPDFDictionaryGetObject(outlineElementRef, "Dest", &destinationRef)) {
CGPDFObjectType destinationType = CGPDFObjectGetType(destinationRef);
最烦的是你有Named Destinations在大多数 pdf 文档中,这需要额外的步骤来解决。我将它们保存在一个数组中,稍后再解析它们。
“正确处理”花了很长时间,因为周围的 PDF 中存在很多差异,即使您按照 PDF 引用实现所有内容,某些文件也无法使用,直到您进一步申请调整。 (PDF 很乱!)
关于ios - 为 iOS 开发时是否可以访问 pdf 大纲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7203565/