使用COCOA将PDF页面转换为图像 [英] Convert PDF pages to images with COCOA

查看:268
本文介绍了使用COCOA将PDF页面转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PDF转换为图片时遇到问题。我想为PDF文档中的每个页面创建一个图像文件。



这是我使用的代码,工作正常。每一页都被转换为图像,但我有图像分辨率的问题。我不知道如何设置输出图像的分辨率。有人可以帮助我吗?

  NSData * pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@http: test / test.pdf]]; 


NSPDFImageRep * img = [NSPDFImageRep imageRepWithData:pdfData];
NSFileManager * fileManager = [NSFileManager defaultManager];
int count = [img pageCount];
for(int i = 0; i [img setCurrentPage:i];
NSImage * temp = [[NSImage alloc] init];
[temp addRepresentation:img];
NSBitmapImageRep * rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]];
NSData * finalData = [rep representationUsingType:NSJPEGFileType properties:nil];
NSString * pageName = [NSString stringWithFormat:@Page_%d.jpg,[img currentPage]];
[fileManager createFileAtPath:[NSString stringWithFormat:@%@ /%@,@/ Users / mac / Desktop /,pageName] content:finalData attributes:nil];非常感谢!


h2_lin>解决方案

最简单的方法是使用ImageIO框架。将PDF数据提交到图片来源< a>获取 CGImage ;将该对象提供给图片目标以生成(并且可选地在相同步骤中保存)JPEG数据。在后面的步骤中,您可以指定图像属性中的分辨率;请参阅个人图片属性



不要忘记完成您的目的地。这是至关重要的。


I am having problem with PDF conversion to images. I would like to create an image file for every page in PDF document.

This is the code I am using and works fine. Every page gets converted into the image, but I have problem with image resolution. I don't know how to set the resolution of the output images. Can someone help me out?

NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost/test/test.pdf"]];


NSPDFImageRep *img = [NSPDFImageRep imageRepWithData:pdfData]; 
 NSFileManager *fileManager = [NSFileManager defaultManager];
 int count = [img pageCount]; 
 for(int i = 0 ; i < count ; i++) { 
  [img setCurrentPage:i]; 
  NSImage *temp = [[NSImage alloc] init]; 
  [temp addRepresentation:img]; 
  NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]]; 
  NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil]; 
  NSString *pageName = [NSString stringWithFormat:@"Page_%d.jpg", [img currentPage]]; 
  [fileManager createFileAtPath:[NSString stringWithFormat:@"%@/%@", @"/Users/mac/Desktop/", pageName] contents:finalData attributes:nil];
 }

Thanks a lot!

解决方案

The simplest way would be to use the ImageIO framework. Feed the PDF data to an image source to get a CGImage; feed that object to an image destination to generate (and optionally save in the same step) the JPEG data. In the latter step, you can specify the resolution among the image properties; see "Individual Image Properties" in the documentation.

Don't forget to finalize your destination. It's vital.

这篇关于使用COCOA将PDF页面转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆