ios - 为 iOS 开发时是否可以访问 pdf 大纲

标签 ios pdf pdfkit outline

由于 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/

相关文章:

ios - 图片不在项目文件夹中

ios - 允许在 iFrame 内缩放,但不允许在 iOS 页面上缩放

html - 在 IE6 和 IE7 的 IFrame 中加载 PDF

cocoa - 是否可以不在使用 PDFKit 生成的 PDF 中嵌入字体?

node.js - PDFKit - 在中心定位图像

python - 方法 : python-pdfkit convert webpage(JS generated) into PDF

ios - 在不显示其根 VC 的情况下显示第 n 个 View Controller

ios - UICollectionView 的 UICollectionViewCell 中的图像在滚动后以编程方式创建重叠

python - 使用 pdfminer 通过 Python 通过 URL 解析 PDF

c++ - 如何在 C++(无 .NET)中将 PDF 转换为位图(栅格化)