开发者

is it possible to access pdf outline when developing for iOS

开发者 https://www.devze.com 2023-03-31 12:56 出处:网络
being that PDFKit is not available on iOS, how is it possible to get the outline of a pdf document in that environment? Is commercia开发者_高级运维l libraries like FastPdfKit or PSPDFKit the only solu

being that PDFKit is not available on iOS, how is it possible to get the outline of a pdf document in that environment? Is commercia开发者_高级运维l libraries like FastPdfKit or PSPDFKit the only solution?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消