Java實現(xiàn)pdf文件合并的使用示例
在maven項目中引入以下依賴包
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-examples</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
創(chuàng)建一個工具類
package org.apache.pdfbox.utils;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.examples.util.PDFMergerExample;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.io.RandomAccessReadMemoryMappedFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author: guanglai.zhou
* @date: 2023/12/14 13:15
*/
public class PdfMergerUtils {
/**
* 合并指定目錄中的pdf文件
*
* @param fromDir 指定目錄
* @param descFile 目標pdf文件
* @return 目標pdf文件
* @throws IOException
*/
public static File merge(String fromDir, String descFile) throws IOException {
final File resultFile = new File(descFile);
File file = new File(fromDir);
List<File> files = new ArrayList<>();
list(file, new Predicate<File>() {
@Override
public boolean test(File file) {
return true;
}
}, new Predicate<File>() {
// 選擇pdf文件
@Override
public boolean test(File file) {
return file.getPath().endsWith(".pdf");
}
}, files);
if (files.isEmpty()) {
throw new RuntimeException("源文件不存在pdf格式文檔?");
}
// files.sort(Comparator.comparing(File::getName));
if (resultFile.exists()) {
FileUtils.forceDelete(resultFile);
}
mergePdfs(resultFile, files);
return resultFile;
}
/**
* 針對文件進行遍歷 如果文件夾滿足directoryPredicate,則繼續(xù)遍歷文件夾,如果是文件,則判斷是否滿足filePredicate,如果滿足則添加到
* collector結果集當中
*
* @param file 文件夾
* @param directoryPredicate 文件夾預期 為null 則不針對文件夾做過濾
* @param filePredicate 文件預期 為null 則不針對文件做過濾
* @param collector 收集器 收集所有符合條件的文件
*/
public static void list(File file, Predicate<File> directoryPredicate, Predicate<File> filePredicate, List<File> collector) {
File[] childFiles = file.listFiles();
if (childFiles == null) {
return;
}
// 根據(jù)腳本名稱進行排序
List<File> fileList = Arrays.stream(childFiles).sorted(Comparator.comparing(File::getName)).collect(Collectors.toList());
for (File childFile : fileList) {
if (childFile.isDirectory()) {
boolean pass = directoryPredicate == null || directoryPredicate.test(childFile);
if (pass) {
// 繼續(xù)遍歷子文件夾目錄
list(childFile, directoryPredicate, filePredicate, collector);
}
} else {
boolean pass = filePredicate == null || filePredicate.test(childFile);
if (pass) {
collector.add(childFile);
}
}
}
}
private static void mergePdfs(File resultFile, List<File> files) throws IOException {
PDFMergerExample example = new PDFMergerExample();
List<RandomAccessRead> sources = new ArrayList<>();
for (File currFile : files) {
sources.add(new RandomAccessReadMemoryMappedFile(currFile));
}
InputStream inputStream = example.merge(sources);
FileUtils.copyInputStreamToFile(inputStream, resultFile);
}
}
將需要合并的pdf文件都拷貝到指定目錄a中,調(diào)用該工具類將該目錄作為第一個參數(shù),第二個參數(shù)傳入輸出文件對象即可。
到此這篇關于Java實現(xiàn)pdf文件合并的使用示例的文章就介紹到這了,更多相關Java pdf文件合并內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring boot+自定義 AOP 實現(xiàn)全局校驗的實例代碼
最近公司重構項目,重構為最熱的微服務框架 spring boot, 重構的時候遇到幾個可以統(tǒng)一處理的問題。這篇文章主要介紹了spring boot+自定義 AOP 實現(xiàn)全局校驗 ,需要的朋友可以參考下2019-04-04
Springboot過濾器禁止ip頻繁訪問功能實現(xiàn)
這篇文章主要介紹了Springboot過濾器禁止ip頻繁訪問功能實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
SpringBoot如何返回Json數(shù)據(jù)格式
這篇文章主要介紹了SpringBoot如何返回Json數(shù)據(jù)格式問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
如何利用 Either 和 Option 進行函數(shù)式錯誤處理
這篇文章主要介紹了如何利用 Either 和 Option 進行函數(shù)式錯誤處理。在 Java 中,錯誤的處理在傳統(tǒng)上由異常以及創(chuàng)建和傳播異常的語言支持進行。但是,如果不存在結構化異常處理又如何呢?,需要的朋友可以參考下2019-06-06
一篇文章帶了解如何用SpringBoot在RequestBody中優(yōu)雅的使用枚舉參數(shù)
這篇文章主要介紹了SpringBoot中RequestBodyAdvice使用枚舉參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
mybatis resultmap 如何為對象賦值的調(diào)用順序
這篇文章主要介紹了mybatis resultmap 如何為對象賦值的調(diào)用順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
一文教會你用mybatis查詢數(shù)據(jù)庫數(shù)據(jù)
MyBatis本身是一個數(shù)據(jù)庫連接框架,可以認為是JDBC的升級版,下面這篇文章主要給大家介紹了關于mybatis查詢數(shù)據(jù)庫數(shù)據(jù)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04
Springboot項目長時間不進行接口操作,提示HikariPool-1警告的解決
這篇文章主要介紹了Springboot項目長時間不進行接口操作,提示HikariPool-1警告的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

