从pdf文件创建目录 [英] Create a table of contents from a pdf file
问题描述
我使用quartz来显示pdf内容,我需要创建一个目录以浏览PDF。从阅读苹果的文档,我想我应该使用CGPDFDocumentGetCatalog,但我找不到任何例子,如何使用这个任何地方。任何想法?
I'm using quartz to display pdf content, and I need to create a table of contents to navigate through the pdf. From reading Apple's documentation I think I am supposed to use CGPDFDocumentGetCatalog, but I can't find any examples on how to use this anywhere. Any ideas?
更新:仍然没有找到解决方案。我累了Alex的解决方案,但我得到的输出看起来像这样:
Update: Still haven't found a solution for this. I tired Alex' solution but the output I get looks like this:
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
对象与标题和匹配页码。
No idea yet how to turn that into a usable table of contents. Ideally I would like to get to an array of NSDictionary
objects with a title and matching page number.
推荐答案
这样可能有助于您开始:
Something like this might help you get started:
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
}
}
这篇关于从pdf文件创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!