is it possible to access pdf outline when developing for iOS

人走茶凉 提交于 2019-12-14 04:07:50

问题


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?


回答1:


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)) {

going down to

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.

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!)




回答2:


It is now possible in iOS 11+.

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

You can get the PDFOutline of a PDFDocument. The PDFOutline's outlineRoot will return outline items if there are any and NULL if none.



来源:https://stackoverflow.com/questions/7203565/is-it-possible-to-access-pdf-outline-when-developing-for-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!