java - 从 pdf 的字节数组中获取 pdf 的第一页作为图像

标签 java image arrays itext pdf-reader

我正在字节数组中获取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/

相关文章:

java - 避免在 Spring Boot 应用程序中向 Sentry 报告 Broken Pipe 错误

java - 通过具有 Activity 实例的类型(类)查找 View

php - 减去两个图像留下结果 - PHP

c++ - 找到具有特定数字(7)的最接近的特定数字

arrays - 如何从图像中删除所有黑色像素并仅使用图像中找到的唯一 RGB 值

java - JaxB 将多个 XMLType 映射到单个类

java - 检测并查明性能回归

javascript - 在 HTML5 Canvas 中处理大图像

javascript - 影响行高的内联图像

arrays - 如何将查询与数组一起使用