关键词:
【中文标题】将 PDF 文件转换为图像【英文标题】:Convert a PDF file to image 【发布时间】:2013-08-13 21:38:29 【问题描述】:我想将 PDF 文档转换为图像。我使用的是 Ghost4j。
问题: Ghost4J 在运行时需要 gsdll32.dll 文件,而我确实不想使用该 dll 文件。
问题一:有没有什么办法可以在ghost4j中不用dll转换图片?
问题 2: 我在 PDFBox API 中找到了解决方案。 org.apache.pdfbox.pdmodel.PDPagep
have method
convertToImage()` 将 PDF 页面转换为图像格式。
PDDocument doc = PDDocument.load(new File("/document.pdf"));
List<PDPage>pages = doc.getDocumentCatalog().getAllPages();
PDPage page = pages.get(0);
BufferedImage image =page.convertToImage();
File outputfile = new File("/image.png");
ImageIO.write(image, "png", outputfile);
doc.close();
我的 PDF 文档只有文本。当我运行此代码时出现异常:
Aug 12, 2013 6:00:24 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: BDC
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.apache.pdfbox.pdmodel.font.PDTrueTypeFont.getawtFont(PDTrueTypeFont.java:481)
at org.apache.pdfbox.pdmodel.font.PDSimpleFont.drawString(PDSimpleFont.java:109)
at org.apache.pdfbox.pdfviewer.PageDrawer.processTextPosition(PageDrawer.java:235)
at org.apache.pdfbox.util.PDFStreamEngine.processEncodedText(PDFStreamEngine.java:496)
at org.apache.pdfbox.util.operator.ShowTextGlyph.process(ShowTextGlyph.java:62)
at org.apache.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:554)
at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:268)
at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:235)
at org.apache.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:215)
at org.apache.pdfbox.pdfviewer.PageDrawer.drawPage(PageDrawer.java:125)
at org.apache.pdfbox.pdmodel.PDPage.convertToImage(PDPage.java:781)
at org.apache.pdfbox.pdmodel.PDPage.convertToImage(PDPage.java:712)
at ge.eid.esignature.adessa.pades.sign.PDFtoImage.main(PDFtoImage.java:25)
Caused by: java.lang.IllegalArgumentException
at java.nio.Buffer.position(Buffer.java:216)
at sun.font.TrueTypeFont.lookupName(TrueTypeFont.java:1153)
at sun.font.TrueTypeFont.getPostscriptName(TrueTypeFont.java:1205)
at java.awt.Font.getPSName(Font.java:1156)
at org.apache.pdfbox.pdmodel.font.FontManager.loadFonts(FontManager.java:101)
at org.apache.pdfbox.pdmodel.font.FontManager.<clinit>(FontManager.java:53)
... 13 more
【问题讨论】:
【参考方案1】:您可以轻松地将04-Request-Headers.pdf文件页面转换为图像格式。
使用 PDF Box 将所有 pdf 页面转换为 Java 中的图像格式。
Apache PDFBox 1.8.* 版本解决方案:
需要罐子pdfbox-1.8.3.jar
或maven依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.8.3</version>
</dependency>
解决方法如下:
package com.pdf.pdfbox.examples;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
@SuppressWarnings("unchecked")
public class ConvertPDFPagesToImages
public static void main(String[] args)
try
String sourceDir = "C:/Documents/04-Request-Headers.pdf"; // Pdf files are read from this folder
String destinationDir = "C:/Documents/Converted_PdfFiles_to_Image/"; // converted images from pdf document are saved here
File sourceFile = new File(sourceDir);
File destinationFile = new File(destinationDir);
if (!destinationFile.exists())
destinationFile.mkdir();
System.out.println("Folder Created -> "+ destinationFile.getAbsolutePath());
if (sourceFile.exists())
System.out.println("Images copied to Folder: "+ destinationFile.getName());
PDDocument document = PDDocument.load(sourceDir);
List<PDPage> list = document.getDocumentCatalog().getAllPages();
System.out.println("Total files to be converted -> "+ list.size());
String fileName = sourceFile.getName().replace(".pdf", "");
int pageNumber = 1;
for (PDPage page : list)
BufferedImage image = page.convertToImage();
File outputfile = new File(destinationDir + fileName +"_"+ pageNumber +".png");
System.out.println("Image Created -> "+ outputfile.getName());
ImageIO.write(image, "png", outputfile);
pageNumber++;
document.close();
System.out.println("Converted Images are saved at -> "+ destinationFile.getAbsolutePath());
else
System.err.println(sourceFile.getName() +" File not exists");
catch (Exception e)
e.printStackTrace();
可以将图像转换为jpg, jpeg, png, bmp, gif
格式。
注意:我提到了主要使用的图像格式。
ImageIO.write(image , "jpg", new File( destinationDir +fileName+"_"+pageNumber+".jpg" ));
ImageIO.write(image , "jpeg", new File( destinationDir +fileName+"_"+pageNumber+".jpeg" ));
ImageIO.write(image , "png", new File( destinationDir +fileName+"_"+pageNumber+".png" ));
ImageIO.write(image , "bmp", new File( destinationDir +fileName+"_"+pageNumber+".bmp" ));
ImageIO.write(image , "gif", new File( destinationDir +fileName+"_"+pageNumber+".gif" ));
控制台输出:
Images copied to Folder: Converted_PdfFiles_to_Image
Total files to be converted -> 13
Aug 06, 2014 1:35:49 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_1.png
Aug 06, 2014 1:35:50 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_2.png
Aug 06, 2014 1:35:51 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_3.png
Aug 06, 2014 1:35:51 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_4.png
Aug 06, 2014 1:35:52 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_5.png
Aug 06, 2014 1:35:52 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_6.png
Aug 06, 2014 1:35:53 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_7.png
Aug 06, 2014 1:35:53 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_8.png
Aug 06, 2014 1:35:54 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_9.png
Aug 06, 2014 1:35:54 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_10.png
Aug 06, 2014 1:35:54 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_11.png
Aug 06, 2014 1:35:55 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_12.png
Aug 06, 2014 1:35:55 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: i
Image Created -> 04-Request-Headers_13.png
Converted Images are saved at -> C:\Documents\Converted_PdfFiles_to_Image
Apache PDFBox 2.0.* 版本解决方案:
必需的罐子pdfbox-2.0.16.jar、fontbox-2.0.16.jar、commons-logging-1.2.jar
或来自 pom.xml 依赖项
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
2.0.16版本解决方案:
package com.pdf.pdfbox.examples;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
/**
*
* @author venkataudaykiranp
*
* @version 2.0.16(Apache PDFBox version support)
*
*/
public class ConvertPDFPagesToImages
public static void main(String[] args)
try
String sourceDir = "C:\\Users\\venkataudaykiranp\\Downloads\\04-Request-Headers.pdf"; // Pdf files are read from this folder
String destinationDir = "C:\\Users\\venkataudaykiranp\\Downloads\\Converted_PdfFiles_to_Image/"; // converted images from pdf document are saved here
File sourceFile = new File(sourceDir);
File destinationFile = new File(destinationDir);
if (!destinationFile.exists())
destinationFile.mkdir();
System.out.println("Folder Created -> "+ destinationFile.getAbsolutePath());
if (sourceFile.exists())
System.out.println("Images copied to Folder Location: "+ destinationFile.getAbsolutePath());
PDDocument document = PDDocument.load(sourceFile);
PDFRenderer pdfRenderer = new PDFRenderer(document);
int numberOfPages = document.getNumberOfPages();
System.out.println("Total files to be converting -> "+ numberOfPages);
String fileName = sourceFile.getName().replace(".pdf", "");
String fileExtension= "png";
/*
* 600 dpi give good image clarity but size of each image is 2x times of 300 dpi.
* Ex: 1. For 300dpi 04-Request-Headers_2.png expected size is 797 KB
* 2. For 600dpi 04-Request-Headers_2.png expected size is 2.42 MB
*/
int dpi = 300;// use less dpi for to save more space in harddisk. For professional usage you can use more than 300dpi
for (int i = 0; i < numberOfPages; ++i)
File outPutFile = new File(destinationDir + fileName +"_"+ (i+1) +"."+ fileExtension);
BufferedImage bImage = pdfRenderer.renderImageWithDPI(i, dpi, ImageType.RGB);
ImageIO.write(bImage, fileExtension, outPutFile);
document.close();
System.out.println("Converted Images are saved at -> "+ destinationFile.getAbsolutePath());
else
System.err.println(sourceFile.getName() +" File not exists");
catch (Exception e)
e.printStackTrace();
【讨论】:
我收到此错误 2015 年 5 月 26 日上午 11:43:31 org.apache.pdfbox.util.PDFStreamEngine processOperator INFO:不支持/禁用操作:BDC 2015 年 5 月 26 日 11:43:31 AM org.apache.pdfbox.util.PDFStreamEngine processOperator 信息:不支持/禁用操作:EMC 我正在使用 pdfbox 1.8.9 jar 最新版PDFbox略有不同。使用 PDFRendered 类。 同时包含文本和图像内容的 pdf 存在问题。我已经看到,在生成最终图像(输入 pdf 文件)后,文本数据被省略,仅显示图像部分(如背景图像等)。感谢您在这方面提供任何帮助。 @yeppe 提供 pdf 文件作为链接。我将为您提供意见。【参考方案2】:您可以尝试使用 NonSequentialParser 来避免某些 PDF 文件出错(带有增量更新):
PDDocument doc = PDDocument.loadNonSeq(new File("/document.pdf"));
【讨论】:
非常感谢,对我很有帮助【参考方案3】:通过 PDFBox 的方式是避免原生绑定的好方法。 尝试使用 PDFBox 中的 PDFImageWriter,我在几行中做了同样的事情,它工作得很好。 您必须提取 PDFDocument 并使用编写器。
PDFImageWriter.write(doc, "png", null, , Integer.MAX_VALUE, "picture");
适用于所有页面。
PDFImageWriter.write(doc, "png", null, 0, 0, "picture");
见: PDFImageWriter Javadoc
【讨论】:
它有同样的例外! :(PDFImageWriter
是否比 ImageIO
更可靠?我更喜欢使用ImageIO
,因为它看起来更简单......除非它不那么可靠
根据我的经验,这不会从 PDF 中写入任何图像,您能确认一下吗? IE。我的 PDF 中有一张图片,但它没有显示在 PNG 中
PDFImageWriter 将生成的图像写入哪里?
ImageIO 是与 PDFBox 2.0 及更高版本一起使用的。请在此处查看迁移指南:pdfbox.apache.org/2.0/migration.html【参考方案4】:
您可能尝试过转换损坏的 PDF 文件。当 PDF 文件包含 JPXEncoded 流时,我遇到了同样的错误。
【讨论】:
几个 PDF 解析器现在有 jbig2 解码器,应该能够处理这个【参考方案5】:您可以使用 PDFBox 轻松地将 PDF 转换为图像。 PDFBox 的PDFRenderer 类的renderImageWithDPI 方法用于将pdf 转换为图像。
PDDocument doc=PDDocument.load(new File("filepath/sample.pdf"));
PDFRenderer pdfRenderer = new PDFRenderer(doc);
BufferedImage bffim = pdfRenderer.renderImageWithDPI(pageNo, 300, ImageType.RGB);
String fileName = "image-" + page + ".png";
ImageIOUtil.writeImage(bim, fileName, 300);
【讨论】:
【参考方案6】: try
PDDocument document = PDDocument.load(PdfInfo.getPDFWAY());
if (document.isEncrypted())
document.decrypt(PdfInfo.getPASSWORD());
if ("bilevel".equalsIgnoreCase(PdfInfo.getCOLOR()))
PdfInfo.setIMAGETYPE( BufferedImage.TYPE_BYTE_BINARY);
else if ("indexed".equalsIgnoreCase(PdfInfo.getCOLOR()))
PdfInfo.setIMAGETYPE(BufferedImage.TYPE_BYTE_INDEXED);
else if ("gray".equalsIgnoreCase(PdfInfo.getCOLOR()))
PdfInfo.setIMAGETYPE(BufferedImage.TYPE_BYTE_GRAY);
else if ("rgb".equalsIgnoreCase(PdfInfo.getCOLOR()))
PdfInfo.setIMAGETYPE(BufferedImage.TYPE_INT_RGB);
else if ("rgba".equalsIgnoreCase(PdfInfo.getCOLOR()))
PdfInfo.setIMAGETYPE(BufferedImage.TYPE_INT_ARGB);
else
System.exit(2);
PDFImageWriter imageWriter = new PDFImageWriter();
boolean success = imageWriter.writeImage(document, PdfInfo.getIMAGE_FORMAT(),PdfInfo.getPASSWORD(),
PdfInfo.getSTART_PAGE(),PdfInfo.getEND_PAGE(),PdfInfo.getOUTPUT_PREFIX(),PdfInfo.getIMAGETYPE(),PdfInfo.getRESOLUTION());
if (!success)
System.exit(1);
document.close();
catch (IOException | CryptographyException | InvalidPasswordException ex)
Logger.getLogger(PdfToImae.class.getName()).log(Level.SEVERE, null, ex);
public class PdfInfo
private static String PDFWAY;
private static String OUTPUT_PREFIX;
private static String PASSWORD;
private static int START_PAGE=1;
private static int END_PAGE=Integer.MAX_VALUE;
private static String IMAGE_FORMAT="jpg";
private static String COLOR="rgb";
private static int RESOLUTION=256;
private static int IMAGETYPE=24;
private static String filename;
private static String filePath="";
【讨论】:
【参考方案7】:对于错误:
org.apache.pdfbox.util.PDFStreamEngine processOperator INFO:不支持/禁用操作
除了 Apache pdfbox jar 之外,您需要在类路径中包含 fontbox-1.7.1 jar,这将解决您的问题,因为 PDFBox 内部使用 fontbox-1.7.1
【讨论】:
“INFO:不支持/禁用的操作”INFO 是无害的,可以忽略。没有人应该使用 1.7.1。当前版本是 2.0.8。pdfbox 将 pdf 转换为图像字节 []
...览了几个示例,唯一能找到的示例描述了如何将转换后的文件直接写入文件系统或将其转换为JavaAWT对象。我宁愿不要产生将图像文件写入文件系统的IO,读入字节[],然后将 查看详情
将图像转换为 pdf 但不保存在文件中
】将图像转换为pdf但不保存在文件中【英文标题】:convertingImagetopdfbutnotsaveinfiles【发布时间】:2019-02-2206:49:21【问题描述】:您好,我正在将pdf从文件打开到imageview,然后在图像视图上我正在删除pin,之后我想再次保存为pdf,... 查看详情
如何通过目标c中的编码将pdf文件转换为图像
】如何通过目标c中的编码将pdf文件转换为图像【英文标题】:Howtoconvertapdffiletoimagethroughcodinginobjectivec【发布时间】:2012-09-1409:30:03【问题描述】:如何通过objectivec中的编码将pdf文件转换为图像我有一个pdf文件,但我想将它保存... 查看详情
使用python将图像文件夹转换为单独的pdf
】使用python将图像文件夹转换为单独的pdf【英文标题】:Convertingimagefolderstoseparatepdfusingpython【发布时间】:2021-07-0220:57:40【问题描述】:我的文件夹包含许多jpg文件。我希望将它们转换为PDF文件。每个文件夹都应该做成一个单... 查看详情
怎么用abbyy将pdf转换为jpeg图像
...面,选择文档转换中的“pdf转图片”选项;二、点击添加文件按钮,将要转换的文件添加进来,再点击开始转换按钮;三、等待一会文件就会被转换完成,将文件下载保存就可以了。按照上述的操作方法,将pdf文件转换成图片就... 查看详情
如何将 PDF 文件页面呈现为图像?
】如何将PDF文件页面呈现为图像?【英文标题】:HowcanIrenderPDFfilepagesasimages?【发布时间】:2010-03-2515:42:20【问题描述】:我需要能够将PDF文件转换为图像(每页一张图像),以便像GoogleDocs那样显示在网页上。这适用于内部Intrane... 查看详情
怎么用abbyy将pdf转换为jpeg图像
...面,选择文档转换中的“pdf转图片”选项;二、点击添加文件按钮,将要转换的文件添加进来,再点击开始转换按钮;三、等待一会文件就会被转换完成,将文件下载保存就可以了。按照上述的操作方法,将pdf文件转换成图片就... 查看详情
将PDF转换为图像的省时方法
...2021-08-0817:44:56【问题描述】:上下文:我有正在处理的PDF文件。我正在使用ocr从这些文档中提取文本,为了能够做到这一点,我必须将我的pdf文件转换为图像。我目前使用pdf2image模块的convert_from_path功能,但它的时间效率非常低... 查看详情
使用 Python 将 PDF 转换为图像
...【问题描述】:我正在尝试在我安装的ubuntu服务器中将pdf文件转换为图像文件:python2.7poppler-utilspdf2image==1.12.1我的代码:frompdf2imageimportconvert_from_path,convert_from_bytesim 查看详情
ImageMagick 命令行:将 PDF 转换为高清图像
...3【问题描述】:我正在尝试使用命令行中的imagemagick将PDF文件即时转换为图像。这些PDF的清晰度非常高,我想获得相关图像(JPG)。通常使用Gimp,当我在这个软件下将这些PDF转换为600dpi 查看详情
将列表中的多个图像合并为 PDF
...【问题描述】:我有数千张图像需要转换并组合成多个PDF文件。一些图像被多次使用。我正在寻找一种自动化的解决方案。我将所有.tif文件命名并组织在一个电子表格中。我想使用该文件列表并运行一个自动化脚本来为自己节... 查看详情
从 pdf 和 word 转换为图像
...略图,我们将文档存储在数据库中,我们需要将其转换为文件流并且必须显示为缩略图。到目前为止,我可以显示图像,因为它是直截了当的,同样我将不得不显示pdf和word文档的图像。我将展示我在图像方面的表现控制器:publi... 查看详情
使用 PHP(ghostscript) 将 PDF 转换为图像耗时太长
...:2013-01-1511:43:46【问题描述】:我有一个php脚本可以将pdf文件转换为一系列jpeg图像。脚本如何实现这一点:-Downloadpdftolocalserver.-Createfolderstoplacejpegima 查看详情
ImageMagick:在不使用外部文本文件的情况下将选择性图像转换为多页 PDF?
】ImageMagick:在不使用外部文本文件的情况下将选择性图像转换为多页PDF?【英文标题】:ImageMagick:ConvertselectiveimagestoMultipagePDFwithoutusingexternaltextfile?【发布时间】:2016-09-2222:25:34【问题描述】:假设我有一个包含5个TIFF文件的目... 查看详情
将fabricjs对象转换为PDF格式而不是简单的图像?
...像格式。有什么方法可以在没有服务器端进程的情况下将文件保存为PDF?【问题讨论】:【参考方案1】:您可以使用canvasAPI的toData 查看详情
为从 PDF 转换为 PNG 的图像创建 zip 文件
】为从PDF转换为PNG的图像创建zip文件【英文标题】:CreateazipfileforimagesconvertedfromPDFtoPNG【发布时间】:2020-06-0110:46:28【问题描述】:我想将从PDF转换为PNG的文件保存在zip文件中。到目前为止,我能够将单个PDF文档转换为PNG格式,... 查看详情
使用 GhostPCL 将带有图像的 PCL 转换为 PDF
...3【问题描述】:我目前正在尝试使用GhostPCL(PCL6)将一些PCL文件转换为PDF。在大多数情况下,这是可行的。但是,某些转换存在一个奇怪的问题。出于某种原因,PCL6没有转换我们文档顶部的某些徽标。标志的格式为:^[(25XABC 查看详情
如何将图像转换为PDF?
】如何将图像转换为PDF?【英文标题】:HowtoconvertImagetoPDF?【发布时间】:2016-07-1806:02:34【问题描述】:我正在开发一个需要将图像转换为PDF的应用程序。我尝试了一些方法,但问题是,该PDF中的图像尺寸非常小。我需要解决方... 查看详情