谁能给我推荐一个免费的 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/