2010-03-31 8 views
5

J'utilise du quartz pour afficher du contenu PDF, et j'ai besoin de créer une table des matières pour naviguer dans le pdf. De la lecture de la documentation d'Apple, je pense que je suis censé utiliser CGPDFDocumentGetCatalog, mais je ne trouve pas d'exemples sur la façon de l'utiliser n'importe où. Des idées?Créer une table des matières à partir d'un fichier pdf

Mise à jour: Nous n'avons pas encore trouvé de solution. Je solution « Alex fatigué, mais la sortie que je reçois ressemble à ceci:

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 

Aucune idée encore comment transformer en une table des matières utilisables. Idéalement, je voudrais obtenir un tableau de NSDictionary objets avec un titre et un numéro de page correspondant.

+0

Ryan, avez-vous trouvé un moyen de créer une table des matières à partir d'un fichier PDF existant? Si oui, veuillez me guider. Je cherche la même chose depuis quelques jours. – DivineDesert

+0

Avez-vous trouvé une solution? Si oui, veuillez ajouter votre réponse ici. – Hemang

Répondre

6

Quelque chose comme cela pourrait vous aider à démarrer:

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 
    }  
} 
+0

Où 'CGPDFDocumentApplyFunction' est-il défini? –

+1

Désolé, cela devrait être 'CGPDFDictionaryApplyFunction'. Voir le fichier d'en-tête 'CGPDFDictionary.h' dans' CoreGraphics.framework'. Merci pour la capture. –

+0

Salut @AlexReynolds, pouvez-vous également convertir votre réponse pour Swift 3? – Hemang

4

Voici mon approche.
1. Téléchargez ce cadre vfr reader
2. Utilisez ces lignes de code pour obtenir tous les chapitres PDF

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

espère que ça vous aidera.

+0

Travaillé parfaitement pour moi en utilisant le lecteur vfr et cet extrait. Très utile. – AndyDunn