将多页PDF文件转换为多张JPG图片
2014-11-07 16:31
627 查看
一开始以为做不到,不过iOS原生的库还是挺强大的。
直接上Code吧
Obj-c代码
-(void) createJPGsFromPDF:(NSString *)fromPDFName
{
if (fromPDFName == nil || [fromPDFName isEqualToString:@""]) {
return;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *docPath = [documentsDir stringByAppendingPathComponent:fromPDFName];
NSURL *fromPDFURL = [NSURL fileURLWithPath:docPath];
CGPDFDocumentRef fromPDFDoc = CGPDFDocumentCreateWithURL((CFURLRef) fromPDFURL);
// Get Total Pages
int pages = CGPDFDocumentGetNumberOfPages(fromPDFDoc);
// Create Folder for store under "Documents/"
NSError *error = nil;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *folderPath = [documentsDir stringByAppendingPathComponent:[fromPDFName stringByDeletingPathExtension]];
[fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
[fileManager release];
int i = 1;
for (i = 1; i <= pages; i++) {
CGPDFPageRef pageRef = CGPDFDocumentGetPage(fromPDFDoc, i);
CGPDFPageRetain(pageRef);
// determine the size of the PDF page
CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
// renders its content.
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef imgContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(imgContext);
CGContextTranslateCTM(imgContext, 0.0, pageRect.size.height);
CGContextScaleCTM(imgContext, 1.0, -1.0);
CGContextSetInterpolationQuality(imgContext, kCGInterpolationDefault);
CGContextSetRenderingIntent(imgContext, kCGRenderingIntentDefault);
CGContextDrawPDFPage(imgContext, pageRef);
CGContextRestoreGState(imgContext);
//PDF Page to image
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Release current source page
CGPDFPageRelease(pageRef);
// Store IMG
NSString *imgname = [NSString stringWithFormat:@"fromPDFName_%d.jpg", i];
NSString *imgPath = [folderPath stringByAppendingPathComponent:imgname];
[UIImageJPEGRepresentation(tempImage, 1.0) writeToFile:imgPath atomically:YES];
}
CGPDFDocumentRelease(fromPDFDoc);
}
转载自/article/4458468.html
直接上Code吧
Obj-c代码
-(void) createJPGsFromPDF:(NSString *)fromPDFName
{
if (fromPDFName == nil || [fromPDFName isEqualToString:@""]) {
return;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *docPath = [documentsDir stringByAppendingPathComponent:fromPDFName];
NSURL *fromPDFURL = [NSURL fileURLWithPath:docPath];
CGPDFDocumentRef fromPDFDoc = CGPDFDocumentCreateWithURL((CFURLRef) fromPDFURL);
// Get Total Pages
int pages = CGPDFDocumentGetNumberOfPages(fromPDFDoc);
// Create Folder for store under "Documents/"
NSError *error = nil;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *folderPath = [documentsDir stringByAppendingPathComponent:[fromPDFName stringByDeletingPathExtension]];
[fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
[fileManager release];
int i = 1;
for (i = 1; i <= pages; i++) {
CGPDFPageRef pageRef = CGPDFDocumentGetPage(fromPDFDoc, i);
CGPDFPageRetain(pageRef);
// determine the size of the PDF page
CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
// renders its content.
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef imgContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(imgContext);
CGContextTranslateCTM(imgContext, 0.0, pageRect.size.height);
CGContextScaleCTM(imgContext, 1.0, -1.0);
CGContextSetInterpolationQuality(imgContext, kCGInterpolationDefault);
CGContextSetRenderingIntent(imgContext, kCGRenderingIntentDefault);
CGContextDrawPDFPage(imgContext, pageRef);
CGContextRestoreGState(imgContext);
//PDF Page to image
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Release current source page
CGPDFPageRelease(pageRef);
// Store IMG
NSString *imgname = [NSString stringWithFormat:@"fromPDFName_%d.jpg", i];
NSString *imgPath = [folderPath stringByAppendingPathComponent:imgname];
[UIImageJPEGRepresentation(tempImage, 1.0) writeToFile:imgPath atomically:YES];
}
CGPDFDocumentRelease(fromPDFDoc);
}
转载自/article/4458468.html
相关文章推荐
- jpg转换成PDF 如何将多张图片合成一个PDF文件
- 怎么把pdf文件内容都转换成jpg图片
- 把PDF文件多张转换为JPG单张
- 怎样可以快速将pdf文件转换为jpg图片
- 将PDF文件转换成为JPG图片的方法
- pdf文件转换成jpg图片格式的四种方法
- 在线将pdf文件转换成jpg图片如何做
- 怎样将PDF文件转换成jpg图片的方法
- pdf文件可以转换成图片吗?怎么将pdf转换成jpg
- 如何将PDF文件转换成为JPG图片
- 怎样将多张jpg图片转成PDF文件来查看
- java 图片文件格式转换(多页tif转jpg 、jpg转tif)
- JAVA 将图片转换成pdf文件
- 支持导出PDF文件并转换成图片格式的PDF查看控件Spire.PDFViewer
- Ubuntu下PDF文件转JPG图片
- 怎么批量把jpg图片转换成pdf
- 如何将多张jpg转换成pdf
- 多个jpg图片怎么转换成pdf文档
- 基于Aspose.Pdf把pdf文件每一页转换为图片
- 转c#,将pdf文件转换成图片文件