java - 在Java中将PDF转换为缩略图

标签 java image pdf

谁能给我推荐一个免费的 Java 库,它可以转换 PDF 并从第一页创建缩略​​图 (PNG)。

谢谢。

最佳答案

你可以试试pdf-renderer它是一个纯 Java 解决方案。以下代码创建第一页的图像。

File pdfFile = new File("/path/to/pdf.pdf");
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
PDFPage page = pdf.getPage(0);

// create the image
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(),
                                 (int) page.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height,
                                  BufferedImage.TYPE_INT_RGB);

Image image = page.getImage(rect.width, rect.height,    // width & height
                            rect,                       // clip rect
                            null,                       // null for the ImageObserver
                            true,                       // fill background with white
                            true                        // block until drawing is done
);
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, null);
ImageIO.write(bufferedImage, format, new File( "/path/to/image.jpg" ));

关于java - 在Java中将PDF转换为缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4929813/

相关文章:

image - 创建神经网络结构的图像

pdf - iText 在 sandbox.stamper.SuperImpose.java 中设置创建日期和修改日期

pdf - 将多个带有链接的 Markdown 文件转换为 PDF

pdf - 为什么 "PDFtk Server"被称为 "PDFtk Server"?

java - 实现 Iterable 的通用类

java - 关闭特定的命令提示符

c++ - Imagemagick C++ : Reducing memory usage

java - Spring MVC 4.2.2 和 Hibernate 5.0.2 给出错误

java - 如何使用 JUnit 和 Mockito 通过静态 util 调用测试 Rest Controller

ios - 如何将图像从 Parse 加载到 Swift 中