Skip to content
Guides

Document Editor

This sample shows how to manipulate multiple pages of a pdf document, including inserting a blank page, inserting a PDF document page, splitting a page, merging a page, deleting a page, rotating a page, replacing a document page, and exporting a document page.

objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
  CPDFDocument *myDocument = document;
  [self insertBlankPage:myDocument];
  [self insertPDFPPage:myDocument];
  [self splitPages:myDocument];
  [self deletePages:myDocument];
  [self rotatePages:myDocument];
  [self repalcePages:myDocument];
  [self extractPages:myDocument];
}

// Insert blank page
- (void)insertBlankPage:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 1: Insert a blank A4-sized page into the sample document\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
    
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFPage"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"InsertBlankPage"];
    
    // Save the document in the test PDF file
    NSURL *insertBlankPageURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:insertBlankPageURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:insertBlankPageURL];
    
    // insert blank page
    [document insertPage:CGSizeMake(595, 852) atIndex:1];
    
    // Save the create insert a blank PDF page action in document
    [document writeToURL:insertBlankPageURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Insert PageIndex : 1\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Size : 595*842\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in InsertBlankPage.pdf\n"];
}

// Insert PDF document
- (void)insertPDFPPage:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 2: Import pages from another document into the example document\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
    
    // Get PDF document
    NSString *filePathTest = [[NSBundle mainBundle] pathForResource:@"text" ofType:@"pdf"];
    CPDFDocument *insertDocument = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePathTest]];
    commandLineStr = [commandLineStr stringByAppendingString:@"Open the document to be imported\n"];
    
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    [indexSet addIndex:0];
    
    // Save the document in the test PDF file
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFPage"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"InsertPDFPage"];
    
    // Save the document in the test PDF file
    NSURL *insertPDFPageURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:insertPDFPageURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:insertPDFPageURL];
    
    // Insert PDF document
    [document importPages:indexSet fromDocument:insertDocument atIndex:1];
    
    // Save the create insert PDF document action in document
    [document writeToURL:insertPDFPageURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in InsertPDFPage.pdf\n"];
}

// Split page
- (void)splitPages:(CPDFDocument *)document {
  NSString *commandLineStr = @"";
  NSMutableArray *splitFilePaths = [NSMutableArray array]
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 3: Split a PDF document into multiple pages\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
    
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFPage"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    
  // Save you split page path in splitFilePaths
    for (int i = 0; i < document.pageCount; i++) {
        CPDFDocument *splitDocument = [[CPDFDocument alloc] init];
        NSIndexSet *index = [[NSIndexSet alloc] initWithIndex:i];
        
        NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@%d.pdf",writeDirectoryPath,@"CommonFivePageSplitPage",i];
        
        [splitFilePaths addObject:writeFilePath];
        
      // Split page
        [splitDocument importPages:index fromDocument:document atIndex:0];
        [splitDocument writeToURL:[NSURL fileURLWithPath:writeFilePath]];
        
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Done. Results saved in CommonFivePageSplitPage%d.pdf\n", i];
    }
  
  // Merge page
  [self mergePages:splitFilePaths];
}

// Merge page
- (void)mergePages:(NSMutableArray *)splitFilePaths {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 4: Merge split documents\n"];
    
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFPage"];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"MergePages"];
    
    CPDFDocument *mergeDocument = [[CPDFDocument alloc] init];
    
    // Copy file
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    
  // Merge the pages you just split
    for (int i = 0; i < splitFilePaths.count; i++) {
        CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:splitFilePaths[i]]];
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Opening CommonFivePageSplitPage%d.pdf\n", i];
      
      // Merge page
        NSIndexSet *index = [[NSIndexSet alloc] initWithIndex:0];
        [mergeDocument importPages:index fromDocument:document atIndex:i];
    }
    
    NSURL *mergePageURL = [NSURL fileURLWithPath:writeFilePath];
    [mergeDocument writeToURL:mergePageURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in MergePages.pdf\n"];
}

// Delete page
- (void)deletePages:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 5: Delete the specified page of the document\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
    
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFPage"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"RemovePages"];
    
    // Save the document in the test PDF file
    NSURL *removePageURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:removePageURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:removePageURL];
    
    // Delete even-numbered pages of a document
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    for (int i = 1; i < document.pageCount; i = i+2) {
        [indexSet addIndex:i];
    }
    
    [document removePageAtIndexSet:indexSet];
    
    // Save the delete even-numbered pages of a document action in document
    [document writeToURL:removePageURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in RemovePages.pdf\n"];
}

// Rotate page
- (void)rotatePages:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 6: Rotate document pages\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
    
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFPage"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"RotatePage"];
    
    // Create a new document for test PDF file
    NSURL *rotatePageURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:rotatePageURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:rotatePageURL];
    
    // Rotate the first page 90 degrees
    CPDFPage *page = [document pageAtIndex:0];
    page.rotation += 90;
    
    // Save the rotate page action in document
    [document writeToURL:rotatePageURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in RotatePage.pdf\n"];
}

// Replace document page
- (void)repalcePages:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 7: Replace specified pages of example documentation with other documentation specified pages\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
    
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFPage"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"ReplacePages"];
    
    // Save the document in the test PDF file
    NSURL *replacePageURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:replacePageURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:replacePageURL];
    
    // Get PDF document
    NSString *filePathTest = [[NSBundle mainBundle] pathForResource:@"text" ofType:@"pdf"];
    CPDFDocument *insertDocument = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePathTest]];
    
    // Replace PDF document
    NSIndexSet *inserSet = [[NSIndexSet alloc]  initWithIndex:0];
    NSIndexSet *removeSet = [[NSIndexSet alloc]  initWithIndex:1];
    [document removePageAtIndexSet:removeSet];
    [document importPages:inserSet fromDocument:insertDocument atIndex:1];
    
    // Save the replace pages action in document
    [document writeToURL:replacePageURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in ReplacePages.pdf\n"];
}

// Export document page
- (void)extractPages:(CPDFDocument *)document {
  NSString *commandLineStr = @"";
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 8: Extract specific pages of a document\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
    
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFPage"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"ExtractPages"];
    
    // Get range of extract page
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    [indexSet addIndex:0];
    [indexSet addIndex:1];
    
    NSURL *extractPageURL = [NSURL fileURLWithPath:writeFilePath];
    
    CPDFDocument *extractDocument = [[CPDFDocument alloc] init];
    [extractDocument importPages:indexSet fromDocument:document atIndex:0];
    
    // Save the extract pages action in document
    [extractDocument writeToURL:extractPageURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in ExtractPages.pdf\n"];
}