使用 Java 将 PDF 转换为图像 [英] PDF to image using Java

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

问题描述

我想将 PDF 页面转换为图像(PNG、JPEG/JPG 或 GIF).我想要整页大小.

I to want convert PDF pages into an image (PNG,JPEG/JPG or GIF). I want them in full-page sizes.

如何使用 Java 完成此操作?哪些库可用于实现这一目标?

How can this be done using Java? What libraries are available for achieving this?

推荐答案

您将需要一个 PDF 渲染器.市场上或多或少有一些不错的(ICEPdf、pdfrenderer),但如果没有,您将不得不依赖外部工具.免费的 PDF 渲染器也无法渲染嵌入的字体,因此只能用于创建缩略图(您最终想要的).

You will need a PDF renderer. There are a few more or less good ones on the market (ICEPdf, pdfrenderer), but without, you will have to rely on external tools. The free PDF renderers also cannot render embedded fonts, and so will only be good for creating thumbnails (what you eventually want).

我最喜欢的外部工具是 Ghostscript,它可以通过单个命令行调用将 PDF 转换为图像.

My favorite external tool is Ghostscript, which can convert PDFs to images with a single command line invocation.

这将 Postscript(和 PDF?)文件转换为 bmp 为我们,作为根据您的需要进行修改的指南(知道您需要环境变量才能让 gs 工作!):<​​/p>

This converts Postscript (and PDF?) files to bmp for us, just as a guide to modify for your needs (Know you need the env vars for gs to work!):

pushd 
setlocal

Set BIN_DIR=C:Program FilesIKOffice_ACMEin
Set GS=C:Program FilesIKOffice_ACMEgs
Set GS_DLL=%GS%gs8.54ingsdll32.dll
Set GS_LIB=%GS%gs8.54lib;%GS%gs8.54Resource;%GS%fonts
Set Path=%Path%;%GS%gs8.54in
Set Path=%Path%;%GS%gs8.54lib

call "%GS%gs8.54ingswin32c.exe" -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE#bmpmono -r600x600 -sOutputFile#%2 -f %1

endlocal
popd

更新: pdfbox 现在能够嵌入字体,因此不再需要 Ghostscript.

UPDATE: pdfbox is now able to embed fonts, so no need for Ghostscript anymore.

这篇关于使用 Java 将 PDF 转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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