Guides
Extract Images in PDF
This sample shows how to extract all the images of a PDF document.
objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
CPDFDocument *myDocument = document;
[self imageExtract:myDocument];
}
// Extract images
- (void)imageExtract:(CPDFDocument *)document {
NSString *commandLineStr = @"";
NSMutableArray *imageFilePaths = [NSMutableArray array];
NSMutableArray *imageNames = [NSMutableArray array];
commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
commandLineStr = [commandLineStr stringByAppendingString:@"Samples 1: Extract all images in the documentn"];
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, @"ImageExtract"];
if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
[[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
// Extract all images from document
NSIndexSet *pages = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, document.pageCount)];
[document extractImageFromPages:pages toPath:writeDirectoryPath];
NSArray *fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:writeDirectoryPath error:nil];
for (NSString *fileName in fileArray) {
NSString *filePath = [writeDirectoryPath stringByAppendingPathComponent:fileName];
commandLineStr = [commandLineStr stringByAppendingFormat:@"%@\n", fileName];
[imageFilePaths addObject:filePath];
[imageNames addObject:fileName];
}
}