在Java中将PDF转换为缩略图 [英] Convert PDF to thumbnail image in Java

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

问题描述

有人可以建议我一个免费的Java库,它可以转换PDF并从第一页创建缩略​​图(PNG)。

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屋!

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