为iOS开发时是否可以访问pdf大纲 [英] is it possible to access pdf outline when developing for iOS

查看:122
本文介绍了为iOS开发时是否可以访问pdf大纲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于PDFKit在iOS上不可用,如何在该环境中获取pdf文档的轮廓?像FastPdfKit或PSPDFKit这样的商业库是唯一的解决方案吗?

being that PDFKit is not available on iOS, how is it possible to get the outline of a pdf document in that environment? Is commercial libraries like FastPdfKit or PSPDFKit the only solution?

推荐答案

访问pdf大纲并不是一件容易的事.我的轮廓解析器具有大约420个LOC.我将发布一些摘要,以便您理解.我无法发布完整的代码,因为它是一个商业库.

It's not TOO tricky to access the pdf outline. My outline parser has about 420 LOC. I'll post some snippets, so you'll get the idea. I can't post the full code as it's a commercial library.

您基本上是这样开始的:

You basically start like this:

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!");
}

您解析这样的单个项目:

you parse single items like this:

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

最烦人的事情是您拥有

The most annoying thing is that you have Named Destinations in most pdf documents, which need additional steps to resolve. I save those in an array and resolve them later.

正确处理"花费了相当长的时间,因为周围存在许多不同的PDF,即使您实施了符合PDF参考的所有内容,某些文件也无法使用,除非您进一步应用调整. (PDF真是一团糟!)

It took quite a while to "get it right" as there are LOTS of differences in the PDFs that are around, and even if you implement everything in compliance to the PDF reference, some files won't work until you apply further tweaking. (PDF is a mess!)

这篇关于为iOS开发时是否可以访问pdf大纲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆