日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java PDF转jpg

發布時間:2023/12/9 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java PDF转jpg 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近需要用到pdf轉圖片,發現不是需要花錢就是有水印限制,不是很好,自己做了個小程序,滿足大家的需求:

maven:

<dependencies><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.5</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox-tools</artifactId><version>2.0.5</version></dependency><dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk16</artifactId><version>1.46</version></dependency></dependencies>

code:

import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.tools.imageio.ImageIOUtil;import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.filechooser.FileSystemView; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.Iterator;/*** Created by Administrator on 2019-02-28.*/ public class PDF2IMG {private static final String PASSWORD = "-password";private static final String START_PAGE = "-startPage";private static final String END_PAGE = "-endPage";private static final String PAGE = "-page";private static final String IMAGE_TYPE = "-imageType";private static final String FORMAT = "-format";private static final String OUTPUT_PREFIX = "-outputPrefix";private static final String PREFIX = "-prefix";private static final String COLOR = "-color";private static final String RESOLUTION = "-resolution";private static final String DPI = "-dpi";private static final String CROPBOX = "-cropbox";private static final String TIME = "-time";public static void main(String[] args) throws IOException {System.setProperty("apple.awt.UIElement", "true");String password = "";String pdfFile = getFile().getPath();String outputFilePath="D:\\XS\\";String outputPrefix = "XSPdfTools";String imageFormat = "jpg";int startPage = 1;int endPage = 2147483647;String color = "rgb";float cropBoxLowerLeftX = 0.0F;float cropBoxLowerLeftY = 0.0F;float cropBoxUpperRightX = 0.0F;float cropBoxUpperRightY = 0.0F;boolean showTime = false;int dpi;try {dpi = Toolkit.getDefaultToolkit().getScreenResolution();} catch (HeadlessException var28) {dpi = 96;}for(int document = 0; document < args.length; ++document) {if(args[document].equals("-password")) {++document;if(document >= args.length) {usage();}password = args[document];} else if(args[document].equals("-startPage")) {++document;if(document >= args.length) {usage();}startPage = Integer.parseInt(args[document]);} else if(args[document].equals("-endPage")) {++document;if(document >= args.length) {usage();}endPage = Integer.parseInt(args[document]);} else if(args[document].equals("-page")) {++document;if(document >= args.length) {usage();}startPage = Integer.parseInt(args[document]);endPage = Integer.parseInt(args[document]);} else if(!args[document].equals("-imageType") && !args[document].equals("-format")) {if(!args[document].equals("-outputPrefix") && !args[document].equals("-prefix")) {if(args[document].equals("-color")) {++document;color = args[document];} else if(!args[document].equals("-resolution") && !args[document].equals("-dpi")) {if(args[document].equals("-cropbox")) {++document;cropBoxLowerLeftX = Float.valueOf(args[document]).floatValue();++document;cropBoxLowerLeftY = Float.valueOf(args[document]).floatValue();++document;cropBoxUpperRightX = Float.valueOf(args[document]).floatValue();++document;cropBoxUpperRightY = Float.valueOf(args[document]).floatValue();} else if(args[document].equals("-time")) {showTime = true;} else if(pdfFile == null) {pdfFile = args[document];}} else {++document;dpi = Integer.parseInt(args[document]);}} else {++document;outputPrefix = args[document];}} else {++document;imageFormat = args[document];}}if(pdfFile == null) {usage();} else {if(outputPrefix == null) {outputPrefix = pdfFile.substring(0, pdfFile.lastIndexOf(46));}PDDocument var30 = null;try {var30 = PDDocument.load(new File(pdfFile), password);ImageType imageType = null;if("bilevel".equalsIgnoreCase(color)) {imageType = ImageType.BINARY;} else if("gray".equalsIgnoreCase(color)) {imageType = ImageType.GRAY;} else if("rgb".equalsIgnoreCase(color)) {imageType = ImageType.RGB;} else if("rgba".equalsIgnoreCase(color)) {imageType = ImageType.ARGB;}if(imageType == null) {System.err.println("Error: Invalid color.");System.exit(2);}if(cropBoxLowerLeftX != 0.0F || cropBoxLowerLeftY != 0.0F || cropBoxUpperRightX != 0.0F || cropBoxUpperRightY != 0.0F) {changeCropBox(var30, cropBoxLowerLeftX, cropBoxLowerLeftY, cropBoxUpperRightX, cropBoxUpperRightY);}long startTime = System.nanoTime();boolean success = true;endPage = Math.min(endPage, var30.getNumberOfPages());PDFRenderer renderer = new PDFRenderer(var30);for(int endTime = startPage - 1; endTime < endPage; ++endTime) {BufferedImage image = renderer.renderImageWithDPI(endTime, (float)dpi, imageType);String duration = outputPrefix + (endTime + 1) + "." + imageFormat;success &= ImageIOUtil.writeImage(image, duration,dpi);}long var31 = System.nanoTime();long var32 = var31 - startTime;int count = 1 + endPage - startPage;if(showTime) {System.err.printf("Rendered %d page%s in %dms\n", new Object[]{Integer.valueOf(count), count == 1?"":"s", Long.valueOf(var32 / 1000000L)});}if(!success) {System.err.println("Error: no writer found for image format \'" + imageFormat + "\'");System.exit(1);}} catch(Exception ex){ex.printStackTrace();}finally {if(var30 != null) {var30.close();}}System.out.println("轉換完成");}}private static void usage() {String message = "Usage: java -jar pdfbox-app-x.y.z.jar PDFToImage [options] <inputfile>\n\nOptions:\n -password <password> : Password to decrypt document\n -format <string> : Image format: " + getImageFormats() + "\n" + " -prefix <string> : Filename prefix for image files\n" + " -page <number> : The only page to extract (1-based)\n" + " -startPage <int> : The first page to start extraction (1-based)\n" + " -endPage <int> : The last page to extract(inclusive)\n" + " -color <int> : The color depth (valid: bilevel, gray, rgb, rgba)\n" + " -dpi <int> : The DPI of the output image\n" + " -cropbox <int> <int> <int> <int> : The page area to export\n" + " -time : Prints timing information to stdout\n" + " <inputfile> : The PDF document to use\n";System.err.println(message);System.exit(1);}private static String getImageFormats() {StringBuilder retval = new StringBuilder();String[] formats = ImageIO.getReaderFormatNames();for(int i = 0; i < formats.length; ++i) {if(formats[i].equalsIgnoreCase(formats[i])) {retval.append(formats[i]);if(i + 1 < formats.length) {retval.append(", ");}}}return retval.toString();}private static void changeCropBox(PDDocument document, float a, float b, float c, float d) {Iterator i$ = document.getPages().iterator();while(i$.hasNext()) {PDPage page = (PDPage)i$.next();System.out.println("resizing page");PDRectangle rectangle = new PDRectangle();rectangle.setLowerLeftX(a);rectangle.setLowerLeftY(b);rectangle.setUpperRightX(c);rectangle.setUpperRightY(d);page.setCropBox(rectangle);}}public static File getFile(){JFileChooser jfc=new JFileChooser(); //設置當前路徑為桌面路徑,否則將我的文檔作為默認路徑FileSystemView fsv = FileSystemView .getFileSystemView();jfc.setCurrentDirectory(fsv.getHomeDirectory());//JFileChooser.FILES_AND_DIRECTORIES 選擇路徑和文件jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );//彈出的提示框的標題jfc.showDialog(new JLabel(), "確定");//用戶選擇的路徑或文件File file=jfc.getSelectedFile();return file;}}

?

總結

以上是生活随笔為你收集整理的java PDF转jpg的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。