您可以轻松地将04-Request-
Headers.pdf文件页面转换为图像格式。
使用PDF Box将所有pdf页面转换为Java中的图像格式。
Apache PDFBox 1.8。*版本的解决方案:
Jar需要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_ImageTotal files to be converted -> 13Aug 06, 2014 1:35:49 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_1.pngAug 06, 2014 1:35:50 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_2.pngAug 06, 2014 1:35:51 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_3.pngAug 06, 2014 1:35:51 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_4.pngAug 06, 2014 1:35:52 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_5.pngAug 06, 2014 1:35:52 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_6.pngAug 06, 2014 1:35:53 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_7.pngAug 06, 2014 1:35:53 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_8.pngAug 06, 2014 1:35:54 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_9.pngAug 06, 2014 1:35:54 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_10.pngAug 06, 2014 1:35:54 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_11.pngAug 06, 2014 1:35:55 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_12.pngAug 06, 2014 1:35:55 PM org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: iImage Created -> 04-Request-Headers_13.pngConverted Images are saved at -> C:documentsConverted_PdfFiles_to_Image
Apache PDFBox 2.0。*版本的解决方案:
所需的Jars
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;public class ConvertPDFPagesToImages { public static void main(String[] args) { try { String sourceDir = "C:\Users\venkataudaykiranp\Downloads-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"; 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(); } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)