简介
pdfbox是Apache开源的一个项目,支持pdf文档操作功能。
官网地址: Apache PDFBox | A Java PDF Library
支持的功能如下图.
引入依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-app</artifactId>
<version>2.0.19</version>
</dependency>
pdf转换成图片
/**
* 经过测试,dpi为96,100,105,120,150,200中,105显示效果较为清晰,体积稳定,dpi越高图片体积越大,一般电脑显示分辨率为96
*/
public static final float DEFAULT_DPI = 105;
/**
* 默认转换的图片格式为jpg
*/
public static final String DEFAULT_FORMAT = "jpg";
/**
* pdf转换成图片
*
* @param pdfPath pdf文件的路径 例如: D:\\test\\test.pdf (2页)
* @param targetPath 输出的图片路径 D:\\test\\
* @return 抽取出来的图片路径数组 Arrays.asList( "D:\\test\\1.jpg","D:\\test\\2.jpg" )
*/
public static List<String> pdfToManyImage(String pdfPath, String targetPath) {
File file = new File(pdfPath);
if (!file.exists()) {
return null;
}
try {
//加载pdf文件
PDDocument doc = PDDocument.load(file);
//读取pdf文件
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
List<String> stringList = new ArrayList<>(pageCount);
String filePath = null;
BufferedImage image;
for (int i = 0; i < pageCount; i++) {
//96/144/198
// Windows native DPI
image = renderer.renderImageWithDPI(i, DEFAULT_DPI);
// BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
filePath = targetPath + (i + 1) + "." + DEFAULT_FORMAT;
//保存图片
ImageIO.write(image, DEFAULT_FORMAT, new File(filePath));
stringList.add(filePath);
}
return stringList;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
图片合成pdf
/**
* 多图片合成pdf的限制后缀
*/
private static final List IMAGE_SUFFIX = Arrays.asList("jpg", "png", "jpeg");
/**
* 多个图片合成一个pdf
*
* @param imgFolder 多图片的文件夹路径 例如:"D:\\image\\"
* @param target 合并的图片路径 "D:\\image\\merge.pdf"
* @throws IOException
*/
public static void manyImageToOnePdf(String imgFolder, String target) throws IOException {
PDDocument doc = new PDDocument();
//创建一个空的pdf文件
doc.save(target);
PDPage page;
PDImageXObject pdImage;
PDPageContentStream contents;
BufferedImage bufferedImage;
String fileName;
float w, h;
String suffix;
File tempFile;
int index;
File folder = new File(imgFolder);
for (int i = 0; i < folder.listFiles().length; i++) {
tempFile = folder.listFiles()[i];
if (!tempFile.isFile()) {
continue;
}
fileName = tempFile.getName();
index = fileName.lastIndexOf(".");
if (index == -1) {
continue;
}
//获取文件的后缀
suffix = fileName.substring(index + 1);
//如果文件后缀不是图片格式,跳过当前循环
if (!IMAGE_SUFFIX.contains(suffix)) {
continue;
}
bufferedImage = ImageIO.read(folder.listFiles()[i]);
//Retrieving the page
pdImage = LosslessFactory.createFromImage(doc, bufferedImage);
w = pdImage.getWidth();
h = pdImage.getHeight();
page = new PDPage(new PDRectangle(w, h));
contents = new PDPageContentStream(doc, page);
contents.drawImage(pdImage, 0, 0, w, h);
System.out.println("Image inserted");
contents.close();
doc.addPage(page);
}
//保存pdf
doc.save(target);
//关闭pdf
doc.close();
}
多个pdf合成1个pdf
/**
* pdf合并拼接
*
* @param files pdf文件列表 例如: Arrays.asList(new File("D:\\1.pdf"),new File("D:\\2.pdf"))
* @param targetPath 合并后的文件 D:\\merge.pdf
* @return 合并后的文件File对象
*/
public static File manyPdfToOne(List<File> files, String targetPath) {
try {
PDFMergerUtility mergePdf = new PDFMergerUtility();
for (File f : files) {
if (f.exists() && f.isFile()) {
// 循环添加要合并的pdf
mergePdf.addSource(f);
}
}
// 设置合并生成pdf文件名称
mergePdf.setDestinationFileName(targetPath);
// 合并pdf
mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
return new File(targetPath);
} catch (Exception e) {
return null;
}
}