在Java中为PDF创建缩略图图像 [英] Create thumbnail image for PDF in Java

查看:1003
本文介绍了在Java中为PDF创建缩略图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a Java library that will can take a PDF and create a thumbnail image (PNG) from the first page.

我已经看过了在JPedal,但其疯狂的许可费完全令人望而却步。我目前正在使用iText来处理PDF文件,但我相信它不会生成缩略图。我可以在命令行上使用类似Ghostscript的东西,但我希望尽可能保持我的项目全Java。

I've already looked at JPedal, but its insane licensing fee is completely prohibitive. I am using iText to manipulate PDF files at the moment, but I believe it doesn't do thumbnail generation. I can use something like Ghostscript on the command line, but I'm hoping to keep my project all-Java if possible.

推荐答案

PDF Renderer 是一个LGPL许可的纯java库,它使这简单(取自他们的示例页面):

PDF Renderer is a LGPL licensed pure-java library that makes this as simple as (taken from their example page):

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创建缩略图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆