Java合并PDF文檔的三種常用方式總結(jié)
前言
Java常用的三種合并pdf的方式
合并PDF
1.pdfbox合并pdf
apache的pdfbox
pom.xml
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.26</version> </dependency>
代碼示例
// 添加待合并文件 for (ZipUtil.RelativeFile sourcePdf : fileRefers) { long time1 = System.currentTimeMillis(); log.info("開(kāi)始添加{}, 大?。簕}MB, 路徑:{}", (++index), FileUtils.newFile(sourcePdf.getFilePath()).length() / 1024 / 1024, sourcePdf.getFilePath()); try (PDDocument document = PDDocument.load(FileUtils.newFile(sourcePdf.getFilePath()), MemoryUsageSetting.setupTempFileOnly()); ) { // 頁(yè)面大綱(1級(jí)) String newName = FileUtils.newFile(sourcePdf.getRelativePath()).getName(); newName = StringUtils.trimToEmpty(newName).replace("." + FilenameUtils.getExtension(newName), ""); PdfBoxBookmark boxData = new PdfBoxBookmark(newName, totalPage); PDDocumentOutline outline = document.getDocumentCatalog().getDocumentOutline(); if (outline != null) { buildBookMark(outline, boxData, totalPage); } / allBookList.add(boxData); mergePdf.addSource(sourcePdf.getFilePath()); // 更新總頁(yè)碼 totalPage += document.getNumberOfPages(); } catch (IOException e) { e.printStackTrace(); log.error("合并pdf失敗:{}",e); throw new BaseException("文件不存在:" + sourcePdf.getFilePath()); } log.info("結(jié)束添加,添加后,總{}頁(yè):{},耗費(fèi):{}秒",totalPage, sourcePdf.getFilePath(), (System.currentTimeMillis() - time1) / 1000); } // 設(shè)置合并后的pdf文件路徑 mergePdf.setDestinationFileName(destFilePath); // 合并pdf try { mergePdf.setDocumentMergeMode(PDFMergerUtility.DocumentMergeMode.OPTIMIZE_RESOURCES_MODE); mergePdf.mergeDocuments(MemoryUsageSetting.setupTempFileOnly()); } catch (IOException e) { e.printStackTrace(); throw new BaseException("合并發(fā)生異常"); }
2.spire.pdf
e-iceblue的spire.pdf
pom.xml
<dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>9.5.6</version> </dependency>
代碼示例:
String[] files = new String[] { "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\error-file.pdf", "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\111.pdf"}; //Merge documents and return an object of PdfDocumentBase PdfDocumentBase pdf = PdfDocument.mergeFiles(files); //Save the result to a PDF file pdf.save("C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\MergedPDF222.pdf", FileFormat.PDF);
3.itextpdf
itextpdf
pom.xml
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13.4</version> </dependency>
代碼示例:
String[] pdfs = new String[] { "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\0001.pdf", "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\0002.pdf"}; String outputPdf = "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\MergedPDF333-.pdf"; // 合并后的PDF文件 try { Document document = new Document(); PdfCopy copy = new PdfCopy(document, new FileOutputStream(outputPdf)); document.open(); for (String pdf : pdfs) { PdfReader reader = new PdfReader(pdf); for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); copy.addPage(copy.getImportedPage(reader, i)); } reader.close(); } document.close(); System.out.println("PDFs merged successfully."); } catch (Exception e) { e.printStackTrace(); }
總結(jié)
1.apache的pdfbox和itextpdf是免費(fèi)的,e-iceblue的spire.pdf是收費(fèi)的而且價(jià)格不菲,無(wú)授權(quán)會(huì)有水印,雖然e-iceblue有免費(fèi)的spire.pdf.free但有頁(yè)數(shù)限制。
2.apache的pdfbox和e-iceblue的spire.pdf對(duì)pdf文檔的容錯(cuò)不高,如果pdf文檔是通過(guò)三方軟件生成或編輯過(guò)的(可能會(huì)存在一定問(wèn)題,但是瀏覽器打開(kāi)或wps打開(kāi)會(huì)容錯(cuò)顯示),在合并時(shí)會(huì)拋文檔的異常。但是itextpdf會(huì)進(jìn)行容錯(cuò)修復(fù)并合并成功。
所以個(gè)人推薦使用itextpdf。
到此這篇關(guān)于Java合并PDF文檔的三種常用方式的文章就介紹到這了,更多相關(guān)Java合并PDF文檔方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Hibernate中Session.get()方法和load()方法的詳細(xì)比較
今天小編就為大家分享一篇關(guān)于Hibernate中Session.get()方法和load()方法的詳細(xì)比較,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03mybatis-plus使用xml自定義sql語(yǔ)句方式
這篇文章主要介紹了mybatis-plus使用xml自定義sql語(yǔ)句方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07springboot+mybaties項(xiàng)目中掃描不到@mapper注解的解決方法
本文主要介紹了springboot+mybaties項(xiàng)目中掃描不到@mapper注解的解決方法,該報(bào)錯(cuò)表明掃描不到Mapper層,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05關(guān)于springboot 配置文件中屬性變量引用方式@@解析
這篇文章主要介紹了關(guān)于springboot 配置文件中屬性變量引用方式@@解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04去掉 IDEA 中 mybatis配置文件的局部背景顏色(圖解)
這篇文章通過(guò)圖文并茂的形式給大家介紹了去掉IntelliJ IDEA 中 mybatis配置文件的局部背景顏色及mybatis 對(duì)應(yīng)的 xml 文件警告的方法圖解,需要的朋友可以參考下2018-09-09Java實(shí)現(xiàn)快速將HTML表格轉(zhuǎn)換成Excel
這篇文章主要為大家詳細(xì)介紹一種使用Java的快速將Web中表格轉(zhuǎn)換成Excel的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05Windows10系統(tǒng)下JDK1.8的下載安裝及環(huán)境變量配置的教程
這篇文章主要介紹了Windows10系統(tǒng)下JDK1.8的下載安裝及環(huán)境變量配置的教程,本文圖文并茂給大家介紹的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03