导入pom文件
dependency>
groupId>org.apache.pdfboxgroupId>
artifactId>fontboxartifactId>
version>2.0.22version>
dependency>
dependency>
groupId>org.apache.pdfboxgroupId>
artifactId>pdfboxartifactId&g服务器托管t;
version>2.0.31version>
dependency>
dependency>
groupId>commons-logginggroupId>
artifactId>commons-loggingartifactId>
version>1.2version>
dependency>
dependency>
groupId>net.sf.cssboxgroupId>
artifactId>pdf2domartifactId>
version>2.0.3version>
dependency>
dependency>
groupId>org.apache.pdfboxgroupId>
artifactId>pdfbox-toolsartifactId>
version>2.0.22version>
dependency>
dependency>
groupId>com.google.code.gsongroupId>
artifactId>gsonartifactId>
version>2.10.1version>
dependency>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
version>4.13.2version>
scope>testscope>
dependency>
dependency>
groupId>com.alibaba.fastjson2groupId>
artifactId>fastjson2artifactId>
version>2.0.49version>
dependency>
整个pdf转换成图片
package pdfToPng;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Pdf2Png {
/**
* 使用pdfbox将整个pdf转换成图片
* 多页pdf会生成多页的图片,后缀会生成图片的位置序号
*
* @param fileAddress 文件地址 如:C:UsersuserDesktoptest
* @param filename PDF文件名不带后缀名
* @param type 图片类型 png和jpg
*/
public static void pdf2png(String fileAddress, String filename, String type) {
long startTime = System.currentTimeMillis();
// 将文件地址和文件名拼接成路径 注意:线上环境不能使用拼接
File file = new File(fileAddress + "" + filename + ".pdf");
try {
// 写入文件
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i pageCount; i++) {
// dpi为144,越高越清晰,转换越慢 Windows native DPI
BufferedImage image = renderer.renderImageWithDPI(i, 144);
// 将图片写出到该路径下
ImageIO.write(image, type, new File(fileAddress + "" + filename + "_" + (i + 1) + "." + type));
}
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + ((endTime - startTime) / 1000.0) + "秒");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
pdf2png("C:UsersMESDesktoptest1", "1", "png");
}
}
自由确定起始页和终止页
package pdfToPng;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Pdf3Png {
/**
* 自由确定起始页和终止页
*
* @param fileAddress 文件地址 如:C:UsersuserDesktoptest
* @param filename PDF文件名不带后缀名
* @param indexOfStart 开始页 开始转换的页码,从0开始
* @param indexOfEnd 结束页 停止转换的页码,-1为全部
* @param type 图片类型 png 和jpg
*/
public static void pdf3png(String fileAddress, String filename, int indexOfStart, int indexOfEnd, String type) {
long startTime = System.currentTimeMillis();
// 将文件地址和文件名拼接成路径 注意:线上环境不能使用拼接
File file = new File(fileAddress + "" + filename + ".pdf");
try {
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = indexOfStart; i indexOfEnd; i++) {
// dpi为144,越高越清晰,转换越慢
BufferedImage image = renderer.renderImageWithDPI(i, 144);
// 将图片写出到该路径下
ImageIO.write(image, type, new File(fileAddress + "" + filename + "_" + (i + 1) + "." + type));
}
long endTime = System.currentTimeMillis();
System.out.println("共耗时:"服务器托管 + ((endTime - startTime) / 1000.0) + "秒");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
pdf3png("C:UsersMESDesktop", "1", 1, 1, "png");
}
}
使用文件流整个pdf转换成图片
package pdfToPng;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
public class Pdf4Png {
/**
* 使用文件流整个pdf转换成图片
*
* @param fileAddress 文件地址 如:C:UsersuserDesktoptest
* @param filename PDF文件名不带后缀名
* @param type 图片类型 png 、jpg
*/
public static void pdfToImage(String fileAddress, String filename, String type) {
long startTime = System.currentTimeMillis();
ListMapString, String>> list = new ArrayList>();
MapString, String> resultMap = null;
PDDocument pdDocument = null;
String fileName = null;
String imgPath = null;
try {
// 将文件地址和文件名拼接成路径 注意:线上环境不能使用拼接
File FilePath = new File(fileAddress + "" + filename + ".pdf");
// 文件流
FileInputStream inputStream = new FileInputStream(FilePath);
int dpi = 296;
pdDocument = PDDocument.load(inputStream);
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pageCount = pdDocument.getNumberOfPages();
/* dpi越大转换后越清晰,相对转换速度越慢 */
for (int i = 0; i pageCount; i++) {
resultMap = new HashMap>();
fileName = filename + "_" + (i + 1) + "." + type;
imgPath = fileAddress + "" + fileName;
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, type, new File(imgPath));
resultMap.put("fileName", fileName);
resultMap.put("filePath", imgPath); // 图片路径
list.add(resultMap);
}
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + ((endTime - startTime) / 1000.0) + "秒"); //转化用时
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 这里需要关闭PDDocument,不然如果想要删除pdf文件时会提示文件正在使用,无法删除的情况
pdDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws FileNotFoundException {
pdfToImage("C:UsersMESDesktop", "1", "png");
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: leetcode – 525. Contiguous Array
Description Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1. Example…