我正在字节数组
中获取pdf。我只想将 pdf 的第一页转换为 image
。
我已经厌倦了 com.lowagie.text.pdf
提供的类(class),如下 -
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+ 的值。让我困惑的是为什么我会得到异常。
异常 -
java.lang.IllegalArgumentException: image == null!
我也尝试过itext,但没有用。
你能建议我一种从 pdf 文件的字节数组中获取第一页图像(第一页作为图像)的方法吗?
最佳答案
据我所知,这对于 iText 是不可能的(至少在我搜索类似问题的前一段时间)。
但是您可以使用 PDFToImage来自Apache PDFBox :
String [] args = new String[7];
args[0] = "-startPage";
args[1] = "1";
args[2] = "-endPage";
args[3] = "1";
args[4] = "-outputPrefix";
args[5] = "MyJpgFile";
args[6] = "MyPdfFile";
PDFToImage.main(args);
为此编写一个包装器很容易。也许与此同时,PDFBox 中就提供了这样的包装器。
关于java - 从 pdf 的字节数组中获取 pdf 的第一页作为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10862928/