Guides
Create an Outline in PDF
This sample shows how to create an outline and get existing outline information.
objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
CPDFDocument *myDocument = document;
[self createOutline:myDocument];
}
// Create outline
- (void)createOutline:(CPDFDocument *)oldDocument {
NSString *commandLineStr = @""
// Get Sandbox path for saving the PDF File
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Outline"];
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"CreateOutlineTest"];
// Save the document in the test PDF file
NSURL *outlineURL = [NSURL fileURLWithPath:writeFilePath];
[oldDocument writeToURL:outlineURL];
// Create a new document for test PDF file
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:outlineURL];
// Create outline root
[document setNewOutlineRoot];
// Get root from PDF document
CPDFOutline *outline = [document outlineRoot];
// Insert a new outline node and set the outline title
CPDFOutline *outlinePage1 = [outline insertChildAtIndex:0];
outlinePage1.label = @"1. page1";
CPDFOutline *outlinePage2 = [outline insertChildAtIndex:1];
outlinePage2.label = @"2. page2";
CPDFOutline *outlinePage3 = [outline insertChildAtIndex:2];
outlinePage3.label = @"3. page3";
CPDFOutline *outlinePage4 = [outline insertChildAtIndex:3];
outlinePage4.label = @"4. page4";
CPDFOutline *outlinePage5 = [outline insertChildAtIndex:4];
outlinePage5.label = @"5. page5";
// Insert secondary directory and set outline text
CPDFOutline *outlinePage1_1 = [outlinePage1 insertChildAtIndex:0];
outlinePage1_1.label = @"1.1 page1_1";
// Save the create outline action in document
[document writeToURL:outlineURL];
commandLineStr = [commandLineStr stringByAppendingString:@"Done.\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in CreateOutlineTest.pdf\n"];
[self printOutline:document];
}
// Print document outline information
- (void)printOutline:(CPDFDocument *)document {
// Get root from PDF document
CPDFOutline *outline = [document outlineRoot];
// Get subdirectory from root
[self loadOutline:outline level:0];
}
// Get subdirectory from root
- (void)loadOutline:(CPDFOutline *)outline level:(NSInteger)level {
NSString *commandLineStr = @""
for (int i=0; i<[outline numberOfChildren]; i++) {
CPDFOutline *data = [outline childAtIndex:i];
CPDFDestination *destination = [data destination];
if (!destination) {
CPDFAction *action = [data action];
if (action && [action isKindOfClass:[CPDFGoToAction class]]) {
destination = [(CPDFGoToAction *)action destination];
}
}
// The previous level directory will be one \t away from the previous level directory
NSString *intervalStr = @"";
for (int j = 0; j < level; j++) {
commandLineStr = [self.commandLineStr stringByAppendingString:@"\t"];
}
commandLineStr = [self.commandLineStr stringByAppendingFormat:@"%@->%@\n", intervalStr, data.label];
[self loadOutline:data level:level+1];
}
}