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項(xiàng)目使用jasypt加解密的方法
jasypt是一個(gè)通用的加解密庫(kù),我們可以使用它在配置文件中對(duì)數(shù)據(jù)庫(kù)密碼進(jìn)行加密,以確保其安全性,接下來(lái)通過(guò)本文給大家介紹SpringBoot項(xiàng)目使用jasypt加解密的方法,感興趣的朋友一起看看吧2022-05-05spring boot+自定義 AOP 實(shí)現(xiàn)全局校驗(yàn)的實(shí)例代碼
最近公司重構(gòu)項(xiàng)目,重構(gòu)為最熱的微服務(wù)框架 spring boot, 重構(gòu)的時(shí)候遇到幾個(gè)可以統(tǒng)一處理的問(wèn)題。這篇文章主要介紹了spring boot+自定義 AOP 實(shí)現(xiàn)全局校驗(yàn) ,需要的朋友可以參考下2019-04-04Springboot過(guò)濾器禁止ip頻繁訪問(wèn)功能實(shí)現(xiàn)
這篇文章主要介紹了Springboot過(guò)濾器禁止ip頻繁訪問(wèn)功能實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04SpringBoot如何返回Json數(shù)據(jù)格式
這篇文章主要介紹了SpringBoot如何返回Json數(shù)據(jù)格式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理
這篇文章主要介紹了如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理。在 Java 中,錯(cuò)誤的處理在傳統(tǒng)上由異常以及創(chuàng)建和傳播異常的語(yǔ)言支持進(jìn)行。但是,如果不存在結(jié)構(gòu)化異常處理又如何呢?,需要的朋友可以參考下2019-06-06一篇文章帶了解如何用SpringBoot在RequestBody中優(yōu)雅的使用枚舉參數(shù)
這篇文章主要介紹了SpringBoot中RequestBodyAdvice使用枚舉參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08mybatis resultmap 如何為對(duì)象賦值的調(diào)用順序
這篇文章主要介紹了mybatis resultmap 如何為對(duì)象賦值的調(diào)用順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01一文教會(huì)你用mybatis查詢數(shù)據(jù)庫(kù)數(shù)據(jù)
MyBatis本身是一個(gè)數(shù)據(jù)庫(kù)連接框架,可以認(rèn)為是JDBC的升級(jí)版,下面這篇文章主要給大家介紹了關(guān)于mybatis查詢數(shù)據(jù)庫(kù)數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Springboot項(xiàng)目長(zhǎng)時(shí)間不進(jìn)行接口操作,提示HikariPool-1警告的解決
這篇文章主要介紹了Springboot項(xiàng)目長(zhǎng)時(shí)間不進(jìn)行接口操作,提示HikariPool-1警告的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12