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

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

问题描述

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

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天全站免登陆