从pdf的字节数组中获取pdf的第一页作为图像 [英] Get the 1st page of a pdf as Image from the Byte Array of the pdf

查看:826
本文介绍了从pdf的字节数组中获取pdf的第一页作为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在字节数组中获得了pdf。我想将pdf的第一页转换为图像

I am getting a pdf in byte array. I want to convert just the 1st page of the pdf into image.

我已经厌倦了 com.lowagie.text.pdf 如下 -

I have tired the classes provided by com.lowagie.text.pdf as follows -

PdfReader reader = new PdfReader(input);
reader.selectPages("1");
File file = new File("D:/img1.jpg");
BufferedImage pdfImage = ImageIO.read(new ByteArrayInputStream(reader.getPageContent(1)));
ImageIO.write(pdfImage, "jpg", file);

ImageIO.write 被叫?
当我获取reader.getPageContent(1)返回的字节数组的大小时,我得到一个1000+的值。令我困惑的是为什么我会得到例外。

Doing this gives me an Exception when ImageIO.write is called? When I fetch the size of the byte array returned by reader.getPageContent(1), I get a 1000+ value. What confuses me is why do I get the Exception.

例外 -

java.lang.IllegalArgumentException: image == null!

我也试过了itext但是没用。

I tried itext as well but it was of no use.

您能否建议我从pdf文件的字节数组中获取第1页图像(第1页图像)?

Could you suggest me a way to get just image of the 1st page (1st page as image) from the byte array of the pdf file?

推荐答案

回答我自己的问题,以便其他人可以从中受益。
经过一番研究后我找到了解决方案。

Answering on my own question so that others can be benefited with it. After some research I found it and got the solution.

看看这个链接

PDFDocumentReader document = new PDFDocumentReader(<byteArraOfThePDF>);
PageDetail pageDetail = new PageDetail("<docIDanything>", "", <pagenumber>, "");
ResourceDetail det = document.getPageAsImage(pageDetail);

BufferedImage image = ImageIO.read(new ByteArrayInputStream(det.getBytes()));
File file = new File("d:/img2.jpg");
ImageIO.write(image, "jpg", file);

这篇关于从pdf的字节数组中获取pdf的第一页作为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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