123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- #import "ReaderDocumentOutline.h"
- #import "CGPDFDocument.h"
- @implementation ReaderDocumentOutline
- #pragma mark - Build option flags
- #define HIERARCHICAL_OUTLINE TRUE
- #pragma mark - ReaderDocumentOutline functions
- void logDictionaryEntry(const char *key, CGPDFObjectRef object, void *info)
- {
-
- NSString *kind = nil;
- CGPDFObjectType type = CGPDFObjectGetType(object);
- switch (type)
- {
- case kCGPDFObjectTypeNull:
- kind = @"CGPDFObjectTypeNull";
- NSLog(@"%s %@", key, kind);
- break;
- case kCGPDFObjectTypeBoolean:
- kind = @"CGPDFObjectTypeBoolean";
- NSLog(@"%s %@", key, kind);
- break;
- case kCGPDFObjectTypeInteger:
- kind = @"CGPDFObjectTypeInteger";
- NSLog(@"%s %@", key, kind);
- break;
- case kCGPDFObjectTypeReal:
- kind = @"CGPDFObjectTypeReal";
- NSLog(@"%s %@", key, kind);
- break;
- case kCGPDFObjectTypeName:
- {
- kind = @"CGPDFObjectTypeName"; const char *pdfName = NULL;
- if (CGPDFObjectGetValue(object, kCGPDFObjectTypeName, &pdfName))
- {
- if (pdfName != NULL) NSLog(@"%s %@ %s", key, kind, pdfName);
- }
- break;
- }
- case kCGPDFObjectTypeString:
- {
- kind = @"CGPDFObjectTypeString"; CGPDFStringRef pdfString = NULL;
- if (CGPDFObjectGetValue(object, kCGPDFObjectTypeString, &pdfString))
- {
- const unsigned char *string = CGPDFStringGetBytePtr(pdfString);
- if (string != NULL) NSLog(@"%s %@ %s", key, kind, string);
- }
- break;
- }
- case kCGPDFObjectTypeArray:
- kind = @"CGPDFObjectTypeArray";
- NSLog(@"%s %@", key, kind);
- break;
- case kCGPDFObjectTypeDictionary:
- kind = @"CGPDFObjectTypeDictionary";
- NSLog(@"%s %@", key, kind);
- break;
- case kCGPDFObjectTypeStream:
- kind = @"CGPDFObjectTypeStream";
- NSLog(@"%s %@", key, kind);
- break;
- }
- }
- #pragma mark - ReaderDocumentOutline class methods
- + (void)logDocumentOutlineArray:(NSArray *)array
- {
- for (DocumentOutlineEntry *item in array)
- {
- NSInteger indent = (item.level * 2);
- NSLog(@"%@%@", [@"" stringByPaddingToLength:indent withString:@" " startingAtIndex:0], item);
- [self logDocumentOutlineArray:item.children];
- }
- }
- + (CGPDFArrayRef)destinationWithName:(const char *)destinationName inDestsTree:(CGPDFDictionaryRef)node
- {
- CGPDFArrayRef destinationArray = NULL;
- CGPDFArrayRef limitsArray = NULL;
- if (CGPDFDictionaryGetArray(node, "Limits", &limitsArray) == true)
- {
- CGPDFStringRef lowerLimit = NULL; CGPDFStringRef upperLimit = NULL;
- if (CGPDFArrayGetString(limitsArray, 0, &lowerLimit) == true)
- {
- if (CGPDFArrayGetString(limitsArray, 1, &upperLimit) == true)
- {
- const char *ll = (const char *)CGPDFStringGetBytePtr(lowerLimit);
- const char *ul = (const char *)CGPDFStringGetBytePtr(upperLimit);
- if ((strcmp(destinationName, ll) < 0) || (strcmp(destinationName, ul) > 0))
- {
- return NULL;
- }
- }
- }
- }
- CGPDFArrayRef namesArray = NULL;
- if (CGPDFDictionaryGetArray(node, "Names", &namesArray) == true)
- {
- NSInteger namesCount = CGPDFArrayGetCount(namesArray);
- for (NSInteger index = 0; index < namesCount; index += 2)
- {
- CGPDFStringRef destName;
- if (CGPDFArrayGetString(namesArray, index, &destName) == true)
- {
- const char *dn = (const char *)CGPDFStringGetBytePtr(destName);
- if (strcmp(dn, destinationName) == 0)
- {
- if (CGPDFArrayGetArray(namesArray, (index + 1), &destinationArray) == false)
- {
- CGPDFDictionaryRef destinationDictionary = NULL;
- if (CGPDFArrayGetDictionary(namesArray, (index + 1), &destinationDictionary) == true)
- {
- CGPDFDictionaryGetArray(destinationDictionary, "D", &destinationArray);
- }
- }
- return destinationArray;
- }
- }
- }
- }
- CGPDFArrayRef kidsArray = NULL;
- if (CGPDFDictionaryGetArray(node, "Kids", &kidsArray) == true)
- {
- NSInteger kidsCount = CGPDFArrayGetCount(kidsArray);
- for (NSInteger index = 0; index < kidsCount; index++)
- {
- CGPDFDictionaryRef kidNode = NULL;
- if (CGPDFArrayGetDictionary(kidsArray, index, &kidNode) == true)
- {
- destinationArray = [self destinationWithName:destinationName inDestsTree:kidNode];
- if (destinationArray != NULL) return destinationArray;
- }
- }
- }
- return NULL;
- }
- + (id)outlineEntryTarget:(CGPDFDictionaryRef)outlineDictionary document:(CGPDFDocumentRef)document
- {
- id entryTarget = nil;
- CGPDFStringRef destName = NULL; const char *destString = NULL;
- CGPDFDictionaryRef actionDictionary = NULL; CGPDFArrayRef destArray = NULL;
- if (CGPDFDictionaryGetDictionary(outlineDictionary, "A", &actionDictionary) == true)
- {
- const char *actionType = NULL;
- if (CGPDFDictionaryGetName(actionDictionary, "S", &actionType) == true)
- {
- if (strcmp(actionType, "GoTo") == 0)
- {
- if (CGPDFDictionaryGetArray(actionDictionary, "D", &destArray) == false)
- {
- CGPDFDictionaryGetString(actionDictionary, "D", &destName);
- }
- }
- else
- {
- if (strcmp(actionType, "URI") == 0)
- {
- CGPDFStringRef uriString = NULL;
- if (CGPDFDictionaryGetString(actionDictionary, "URI", &uriString) == true)
- {
- const char *uri = (const char *)CGPDFStringGetBytePtr(uriString);
- entryTarget = [NSURL URLWithString:[NSString stringWithCString:uri encoding:NSASCIIStringEncoding]];
- }
- }
- }
- }
- }
- else
- {
- if (CGPDFDictionaryGetArray(outlineDictionary, "Dest", &destArray) == false)
- {
- if (CGPDFDictionaryGetString(outlineDictionary, "Dest", &destName) == false)
- {
- CGPDFDictionaryGetName(outlineDictionary, "Dest", &destString);
- }
- }
- }
- if (destName != NULL)
- {
- CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(document);
- CGPDFDictionaryRef namesDictionary = NULL;
- if (CGPDFDictionaryGetDictionary(catalogDictionary, "Names", &namesDictionary) == true)
- {
- CGPDFDictionaryRef destsDictionary = NULL;
- if (CGPDFDictionaryGetDictionary(namesDictionary, "Dests", &destsDictionary) == true)
- {
- const char *destinationName = (const char *)CGPDFStringGetBytePtr(destName);
- destArray = [self destinationWithName:destinationName inDestsTree:destsDictionary];
- }
- }
- }
- if (destString != NULL)
- {
- CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(document);
- CGPDFDictionaryRef destsDictionary = NULL;
- if (CGPDFDictionaryGetDictionary(catalogDictionary, "Dests", &destsDictionary) == true)
- {
- CGPDFDictionaryRef targetDictionary = NULL;
- if (CGPDFDictionaryGetDictionary(destsDictionary, destString, &targetDictionary) == true)
- {
- CGPDFDictionaryGetArray(targetDictionary, "D", &destArray);
- }
- }
- }
- if (destArray != NULL)
- {
- NSInteger targetPageNumber = 0;
- CGPDFDictionaryRef pageDictionaryFromDestArray = NULL;
- if (CGPDFArrayGetDictionary(destArray, 0, &pageDictionaryFromDestArray) == true)
- {
- NSInteger pageCount = CGPDFDocumentGetNumberOfPages(document);
- for (NSInteger pageNumber = 1; pageNumber <= pageCount; pageNumber++)
- {
- CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, pageNumber);
- CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
- if (pageDictionaryFromPage == pageDictionaryFromDestArray)
- {
- targetPageNumber = pageNumber; break;
- }
- }
- }
- else
- {
- CGPDFInteger pageNumber = 0;
- if (CGPDFArrayGetInteger(destArray, 0, &pageNumber) == true)
- {
- targetPageNumber = (pageNumber + 1);
- }
- }
- if (targetPageNumber > 0)
- {
- entryTarget = [NSNumber numberWithInteger:targetPageNumber];
- }
- }
- return entryTarget;
- }
- + (void)outlineItems:(CGPDFDictionaryRef)outlineDictionary document:(CGPDFDocumentRef)document array:(NSMutableArray *)array level:(NSInteger)level
- {
- do
- {
- DocumentOutlineEntry *outlineEntry = nil;
- CGPDFStringRef string = NULL;
- if (CGPDFDictionaryGetString(outlineDictionary, "Title", &string) == true)
- {
- CFStringRef title = NULL;
- if ((title = CGPDFStringCopyTextString(string)) != NULL)
- {
- NSString *titleString = (__bridge NSString *)title;
- id entryTarget = [self outlineEntryTarget:outlineDictionary document:document];
- NSString *trimmed = [titleString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- outlineEntry = [DocumentOutlineEntry newWithTitle:trimmed target:entryTarget level:level];
- [array addObject:outlineEntry]; CFRelease(title);
- }
- }
- if (outlineEntry != nil)
- {
- CGPDFDictionaryRef childItem = NULL;
- if (CGPDFDictionaryGetDictionary(outlineDictionary, "First", &childItem) == true)
- {
- #if (HIERARCHICAL_OUTLINE == TRUE)
- NSMutableArray *childArray = [NSMutableArray array]; outlineEntry.children = childArray;
- [self outlineItems:childItem document:document array:childArray level:(level + 1)];
- #else
- [self outlineItems:childItem document:document array:array level:(level + 1)];
- #endif
- }
- }
- } while (CGPDFDictionaryGetDictionary(outlineDictionary, "Next", &outlineDictionary) == true);
- }
- + (NSArray *)outlineFromFileURL:(NSURL *)fileURL password:(NSString *)phrase
- {
- NSMutableArray *outlineArray = nil;
- if ((fileURL != nil) && [fileURL isFileURL])
- {
- CGPDFDocumentRef document = CGPDFDocumentCreateUsingUrl((__bridge CFURLRef)fileURL, phrase);
- if (document != NULL)
- {
- CGPDFDictionaryRef outlines = NULL;
- CGPDFDictionaryRef catalog = CGPDFDocumentGetCatalog(document);
- if (CGPDFDictionaryGetDictionary(catalog, "Outlines", &outlines) == true)
- {
- CGPDFDictionaryRef firstItem = NULL;
- if (CGPDFDictionaryGetDictionary(outlines, "First", &firstItem) == true)
- {
- outlineArray = [NSMutableArray array];
- [self outlineItems:firstItem document:document array:outlineArray level:0];
- }
- }
- CGPDFDocumentRelease(document);
- }
- }
-
- return [outlineArray copy];
- }
- @end
- #pragma mark -
- @interface DocumentOutlineEntry ()
- @property (nonatomic, assign, readwrite) NSInteger level;
- @property (nonatomic, strong, readwrite) NSString *title;
- @property (nonatomic, strong, readwrite) id target;
- @end
- @implementation DocumentOutlineEntry
- {
- NSInteger _level;
- NSMutableArray *_children;
- NSString *_title;
- id _target;
- }
- #pragma mark - Properties
- @synthesize level = _level;
- @synthesize children = _children;
- @synthesize target = _target;
- @synthesize title = _title;
- #pragma mark - DocumentOutlineEntry class methods
- + (instancetype)newWithTitle:(NSString *)title target:(id)target level:(NSInteger)level
- {
- return [[DocumentOutlineEntry alloc] initWithTitle:title target:target level:level];
- }
- #pragma mark - DocumentOutlineEntry instance methods
- - (instancetype)initWithTitle:(NSString *)title target:(id)target level:(NSInteger)level
- {
- if ((self = [super init]))
- {
- self.title = title; self.target = target; self.level = level;
- }
- return self;
- }
- - (NSString *)description
- {
- NSString *format = @"%@ Title = '%@', Target = '%@', Level = (%i)";
- return [[NSString alloc] initWithFormat:format, [super description], _title, _target, _level];
- }
- @end
|