【问题标题】:Create a table of contents from a pdf file从 pdf 文件创建目录
【发布时间】:2011-02-03 02:56:02
【问题描述】:

我正在使用quartz 来显示pdf 内容,我需要创建一个目录来浏览pdf。通过阅读 Apple 的文档,我认为我应该使用 CGPDFDocumentGetCatalog,但我找不到任何关于如何在任何地方使用它的示例。有什么想法吗?

更新:仍然没有找到解决方案。我厌倦了 Alex 的解决方案,但我得到的输出如下所示:

2011-07-27 09:16:19.359 LDS Scriptures App-iPad[624:707] key: Pages
2011-07-27 09:16:19.361 LDS Scriptures App-iPad[624:707] key: Count
2011-07-27 09:16:19.362 LDS Scriptures App-iPad[624:707] pdf integer value: 238
2011-07-27 09:16:19.363 LDS Scriptures App-iPad[624:707] key: Kids
2011-07-27 09:16:19.366 LDS Scriptures App-iPad[624:707] key: Type
2011-07-27 09:16:19.368 LDS Scriptures App-iPad[624:707] key: Outlines
2011-07-27 09:16:19.370 LDS Scriptures App-iPad[624:707] key: Count
2011-07-27 09:16:19.371 LDS Scriptures App-iPad[624:707] pdf integer value: 7
2011-07-27 09:16:19.372 LDS Scriptures App-iPad[624:707] key: First
2011-07-27 09:16:19.374 LDS Scriptures App-iPad[624:707] key: Parent
2011-07-27 09:16:19.375 LDS Scriptures App-iPad[624:707] key: Count
2011-07-27 09:16:19.376 LDS Scriptures App-iPad[624:707] pdf integer value: 7

还不知道如何把它变成一个可用的目录。理想情况下,我想获得具有标题和匹配页码的 NSDictionary 对象数组。

【问题讨论】:

  • Ryan,您有没有找到从现有 PDF 创建 TOC 的方法?如果是这样,请指导我。几天以来我一直在寻找相同的东西。
  • 你找到解决办法了吗?如果是,请在此处添加您的答案。

标签: iphone objective-c cocoa-touch quartz-2d


【解决方案1】:

这样的事情可能会帮助您入门:

NSURL *documentURL = ...; // URL to file or http resource etc.
CGPDFDocumentRef pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef)documentURL);
CGPDFDictionaryRef pdfDocDictionary = CGPDFDocumentGetCatalog(pdfDocument);
// loop through dictionary...
CGPDFDictionaryApplyFunction(pdfDocDictionary, ListDictionaryObjects, NULL);
CGPDFDocumentRelease(pdfDocument);

...

void ListDictionaryObjects (const char *key, CGPDFObjectRef object, void *info) {
    NSLog("key: %s", key);
    CGPDFObjectType type = CGPDFObjectGetType(object);
    switch (type) { 
        case kCGPDFObjectTypeDictionary: {
            CGPDFDictionaryRef objectDictionary;
            if (CGPDFObjectGetValue(object, kCGPDFObjectTypeDictionary, &objectDictionary)) {
                CGPDFDictionaryApplyFunction(objectDictionary, ListDictionaryObjects, NULL);
            }
        }
        case kCGPDFObjectTypeInteger: {
            CGPDFInteger objectInteger;
            if (CGPDFObjectGetValue(object, kCGPDFObjectTypeInteger, &objectInteger)) {
                NSLog("pdf integer value: %ld", (long int)objectInteger); 
            }
        }
        // test other object type cases here
        // cf. http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CGPDFObject/Reference/reference.html#//apple_ref/doc/uid/TP30001117-CH3g-SW1
    }    
}

【讨论】:

  • CGPDFDocumentApplyFunction 定义在哪里?
  • 抱歉,应该是CGPDFDictionaryApplyFunction。请参阅CoreGraphics.framework 中的CGPDFDictionary.h 头文件。谢谢你的收获。
  • 嗨@AlexReynolds,你能把你的答案也转换成 Swift 3 吗?
【解决方案2】:

这是我的方法。
1。下载这个框架vfr reader
2。使用这行代码获取所有PDF章节

NSString *path = [[NSBundle mainBundle] pathForResource:@"Reader" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSArray *arr = [ReaderDocumentOutline outlineFromFileURL:targetURL password:@""];

希望对你有帮助。

【讨论】:

  • 使用 vfr 阅读器和这个 sn-p 非常适合我。非常有用。
【解决方案3】:

我使用 iOS 的免费软件库 FastPdfKit

【讨论】:

    【解决方案4】:

    您使用 PDFKit 的 PDFOutline 对象创建目录(AKA 大纲、书签)。您还可以阅读 PDF 中的现有大纲。

    CGPDFDocumentGetCatalog是一种获取文件中PDF数据目录的方法,它比人类的“内容”概念要低得多。

    在没有现有大纲的情况下,我不确定是否有办法从 PDF 流本身获取有意义的数据,例如试图仅从 PDF 数据中了解章节标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-17
      • 2011-01-15
      • 2014-03-03
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 2013-03-26
      相关资源
      最近更新 更多