iOS では PDFKit を使用できないため、その環境で PDF ドキュメントのアウトラインを取得するにはどうすればよいですか? FastPdfKit や PSPDFKit などの商用ライブラリが唯一の解決策ですか?
1059 次
2 に答える
3
PDF の概要にアクセスするのはそれほど難しいことではありません。私のアウトライン パーサーには約 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);
最も厄介なことは、ほとんどの PDF ドキュメントに名前付きの宛先があることです。これを解決するには、追加の手順が必要です。それらを配列に保存し、後で解決します。
周りにあるPDFには多くの違いがあるため、「正しく理解する」のにかなりの時間がかかりました.PDFリファレンスに準拠してすべてを実装したとしても、さらに微調整を適用するまで一部のファイルは機能しません. (PDFはめちゃくちゃです!)
于 2012-03-11T06:11:27.853 に答える