我正在寻找可以获取 PDF 并从第一页创建缩略图 (PNG) 的 Java 库。
我已经看过 JPedal,但其疯狂的许可费用令人望而却步。我目前正在使用 iText 来处理 PDF 文件,但我相信它不会生成缩略图。我可以在命令行上使用 Ghostscript 之类的东西,但我希望尽可能让我的项目全部使用 Java。
最佳答案
PDF Renderer是一个 LGPL 许可的纯 java 库,它使这变得像(取自他们的示例页面)一样简单:
File file = new File("test.pdf");
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
// draw the first page to an image
PDFPage page = pdffile.getPage(0);
//get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0,0,
(int)page.getBBox().getWidth(),
(int)page.getBBox().getHeight());
//generate the image
Image img = 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
);
关于java - 在 Java 中为 PDF 创建缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2844961/