Java實(shí)現(xiàn)pdf文件合并的使用示例
在maven項(xiàng)目中引入以下依賴包
<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)建一個(gè)工具類
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 目標(biāo)pdf文件
* @return 目標(biāo)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;
}
/**
* 針對(duì)文件進(jìn)行遍歷 如果文件夾滿足directoryPredicate,則繼續(xù)遍歷文件夾,如果是文件,則判斷是否滿足filePredicate,如果滿足則添加到
* collector結(jié)果集當(dāng)中
*
* @param file 文件夾
* @param directoryPredicate 文件夾預(yù)期 為null 則不針對(duì)文件夾做過(guò)濾
* @param filePredicate 文件預(yù)期 為null 則不針對(duì)文件做過(guò)濾
* @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ù)腳本名稱進(jìn)行排序
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)用該工具類將該目錄作為第一個(gè)參數(shù),第二個(gè)參數(shù)傳入輸出文件對(duì)象即可。
到此這篇關(guān)于Java實(shí)現(xiàn)pdf文件合并的使用示例的文章就介紹到這了,更多相關(guān)Java pdf文件合并內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot配置文件中數(shù)據(jù)庫(kù)密碼加密兩種方案(推薦)
SpringBoot項(xiàng)目經(jīng)常將連接數(shù)據(jù)庫(kù)的密碼明文放在配置文件里,安全性就比較低一些,尤其在一些企業(yè)對(duì)安全性要求很高,因此我們就考慮如何對(duì)密碼進(jìn)行加密,文中給大家介紹加密的兩種方式,感興趣的朋友一起看看吧2019-10-10
IDEA2020.1創(chuàng)建springboot項(xiàng)目(國(guó)內(nèi)腳手架)安裝lombok
這篇文章主要介紹了IDEA2020.1創(chuàng)建springboot項(xiàng)目(國(guó)內(nèi)腳手架)安裝lombok,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
java同步器AQS架構(gòu)AbstractQueuedSynchronizer原理解析
這篇文章主要為大家介紹了java同步器AQS架構(gòu)AbstractQueuedSynchronizer的底層原理及源碼解析,有需要的朋友可以借鑒參考下,希望能有所幫助,祝大家多多進(jìn)步早日升職加薪2022-03-03
Java實(shí)現(xiàn)定時(shí)器的四種方式
這篇文章主要介紹了Java實(shí)現(xiàn)定時(shí)器的四種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot如何連接兩個(gè)數(shù)據(jù)庫(kù)(多個(gè))
這篇文章主要介紹了springboot如何連接兩個(gè)數(shù)據(jù)庫(kù)(多個(gè)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印
這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲
這篇文章主要介紹了Java實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
關(guān)于Socket的解析以及雙方即時(shí)通訊的java實(shí)現(xiàn)方法
本篇文章主要介紹了關(guān)于Socket的解析以及雙方通訊的java實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

