在Java中为PDF创建缩略图

我正在寻找一个Java库,该库可以获取PDF并从第一页创建缩略​​图(PNG)。

我已经看过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中为PDF创建缩略图 的全部内容, 来源链接: utcz.com/qa/424803.html

回到顶部