是否可以访问开发iOS时的pdf大纲

由于PDFKit在iOS上不可用,怎样才能在该环境下获得PDF文档的大纲呢? 像FastPdfKit或PSPDFKit商业图书馆是唯一的解决scheme?

访问pdf大纲不是太棘手。 我的大纲parsing器有大约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!"); } 

你parsing这样的单个项目:

 // 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); 

最烦人的是,你在大多数PDF文件中有命名目的地 ,这需要额外的步骤来解决。 我将它们保存在一个数组中,稍后解决。

由于PDF文件中存在大量的差异,因此花费了相当长的一段时间,即使您按照PDF参考实现了所有内容,但有些文件在您进行进一步调整之前无法运行。 (PDF是一团糟!)

现在可以在iOS 11+中使用。

https://developer.apple.com/documentation/pdfkit

您可以获取PDFDocument的PDFOutline。 如果有的话,PDFOutline的outlineRoot将返回大纲项目,如果没有,则返回NULL。