Series Article Directory
Article list |
---|
1. springboot operation pdf (1) word to pdf |
Article directory
foreword
In the previous article, what is a PDF file was introduced in detail, so I won't introduce it in detail here. But there is a new problem, how to merge multiple pdf files into one pdf file in the java backend?
1. What is pdfbox?
The Apache PDFBox library is an open source Java tool for manipulating PDF documents. The project allows creating new PDF documents, manipulating existing documents, and extracting content from documents. ApachePDFBox also includes several command line utilities. Apache PDFBox is released under the Apache License v2.0.
This is the official website's introduction to pdfbox. pdfbox official website
2. Use steps
1. Import dependencies
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.25</version>
</dependency>
2. The code for merging pdf is as follows
The code is as follows (example):
public static void MulFilePdfToOne(List<File> files, String targetPath) {
PDFMergerUtility mergePdf = new PDFMergerUtility();
for (File f : files) {
// 循环添加要合并的pdf
if(f.exists() && f.isFile()){
try {
mergePdf.addSource(f);
mergePdf.setDestinationFileName(targetPath);// 设置合并生成pdf文件名称
mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());// 合并pdf
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
Summarize
pdfbox has many effects in merging pdf files, and the test can merge 27 pdf files of 100M at the same time. I personally think that it can meet the general needs of merging pdf files.
If you want to know more about how to use it, or get the java code, please pay attention to the official account.