在 Java 中将 PDF 转换为缩略图 [英] Convert PDF to thumbnail image in Java
本文介绍了在 Java 中将 PDF 转换为缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能给我推荐一个可以转换 PDF 并从第一页创建缩略图 (PNG) 的免费 Java 库.
Can anybody suggest me a free Java library that can convert a PDF and create a thumbnail image (PNG) from the first page.
谢谢.
推荐答案
你可以试试 pdf-renderer 它是一个纯 Java 解决方案.以下代码创建第一页的图像.
You could try pdf-renderer it is a pure java solution. The following Code creates an image of the first page.
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 中将 PDF 转换为缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文