Skip to content
Guides

Flatten PDF

This sample shows how to create a copy of Flattened.

objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
  CPDFDocument *myDocument = document;
  [self flattenedCopy:myDocument];
}

// Flattened copy
- (void)flattenedCopy:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    // Get Sandbox path for saving the PDF File
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"FlattenedCopy"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"FlattenedCopyTest"];
    
    // Save the document in the test PDF file
    NSURL *flattenedCopyURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:flattenedCopyURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:flattenedCopyURL];
    
    // Save flatten copy of document
    [document writeFlattenToURL:flattenedCopyURL];

    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in FlattenedCopyTest.pdf\n"];
}