如何将 PDF 文件转换为图像?
我让用户能够将图像上传到我的 Air 应用程序,然后在图像控件中显示该图像。但我需要允许以相同的方式上传 PDF,因此我需要将 PDF 转换为图像。我现在只关心他们上传的 PDF 的第一页。
我正在做的是: 1) 用户使用文件引用控件浏览文件 2) 用户选择要上传的图像或PDF 3)我将所述图像编码为base64并存储它 4) 然后我从该 base64 加载类似的内容:
public function decodeImage(b64String:String):void{
var decoder:Base64Decoder = new Base64Decoder();
decoder.decode(b64String);
var imgLoader:Loader = new Loader();
imgLoader.loadBytes(decoder.toByteArray());
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,decodedImageLoaded);
}
private function decodedImageLoaded(event:Event):void{
var imgLoader:Loader = (event.target as LoaderInfo).loader;
var bmp:Bitmap = Bitmap(imgLoader.content);
imgLoader.removeEventListener(Event.COMPLETE, decodedImageLoaded);
var img:Image = new Image();
img.source = bmp;
this.addChild(img);
}
此方法非常适合 .gif、.jpg 和 .png。在我的过程中的某个时刻,可能在初始上传时我需要将 PDF 的第一页转换为 png,以便我可以使用此过程的其余部分。
我欢迎任何想法,唯一的要求是它必须是 Air 应用程序的一部分,我不能使用在服务器上运行的 ImageMagick 之类的东西,但我可以使用可以编译到成品中的组件。
I'm giving users the ability to upload an image to my Air app, then displaying this image in an image control. But I need to allow for PDF uploading in the same manner, so I need to convert the PDF to an image. I only care about the first page of the PDF they upload for now.
What I'm doing is:
1) User browses for a file with the file reference control
2) User chooses the image or PDF to upload
3) I encode said image to base64 and store it
4) I then load from that base64 with something like:
public function decodeImage(b64String:String):void{
var decoder:Base64Decoder = new Base64Decoder();
decoder.decode(b64String);
var imgLoader:Loader = new Loader();
imgLoader.loadBytes(decoder.toByteArray());
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,decodedImageLoaded);
}
private function decodedImageLoaded(event:Event):void{
var imgLoader:Loader = (event.target as LoaderInfo).loader;
var bmp:Bitmap = Bitmap(imgLoader.content);
imgLoader.removeEventListener(Event.COMPLETE, decodedImageLoaded);
var img:Image = new Image();
img.source = bmp;
this.addChild(img);
}
This method is working great for .gif, .jpg, and .png. At some point in my process, probably the initial upload I need to convert the first page of a PDF to a png so that I can use the rest of this process.
I welcome any ideas with the sole requirement being that it has to be a part of the Air app, I can't use something like ImageMagick that runs on a server, but I could use a component that I can compile in to the finished product.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信 AlivePDF for Flash 现在具有读取 PDF 文件的功能。您也可以尝试 PurePDF。您可以使用其中的一些来获取所需的页面并将其转换为图像。
I believe AlivePDF for Flash now has capabilities to read a PDF file. You might try PurePDF, as well. You could potentially use ones of these to get that desired page and convert it to an image.
您见过 swftools 吗?它能够将 PDF 转换为 SWF、PNG、JPG 等...
Have you seen swftools? It has the ability to convert a PDF to a SWF, PNG, JPG, etc...