[Java]利用itextpdf将多个多页的PDF合并为一个
生活随笔
收集整理的這篇文章主要介紹了
[Java]利用itextpdf将多个多页的PDF合并为一个
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在前一篇文章,介紹了如何將圖片(JPG,PNG,TIF多頁文件)文件合并為一個PDF,并輸出到web網(wǎng)頁或文件。
本次介紹如何將多個PDF(包含多頁)合并輸出為一個PDF(輸出為web網(wǎng)頁也一樣,只要把OutputStream改成ByteArrayOutputStream并返回bytes就可以了)
首先引入itextpdf依賴,編輯pom.xml,增加如下dependency
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.2</version> </dependency>下面直接上方法:
public static void mergePdf(File[] pdfFiles) throws Exception {Document document = new Document();document.setMargins(0, 0, 0, 0);// ByteArrayOutputStream bos = new ByteArrayOutputStream();OutputStream bos = new FileOutputStream("/path/to/dest_pdf_file.pdf");PdfCopy copy = new PdfCopy(document, bos);document.open();for (File file : pdfFiles) {PdfReader reader = new PdfReader(file.getAbsolutePath());copy.addDocument(reader);copy.freeReader(reader);reader.close();}document.close();}如何將圖片(JPG,PNG,TIF多頁文件)文件合并為一個PDF,并輸出到web網(wǎng)頁或文件,請移步:https://blog.terrynow.com/2021/01/20/spring-springboot-output-pdf-from-jpg-png-tif-images/
總結(jié)
以上是生活随笔為你收集整理的[Java]利用itextpdf将多个多页的PDF合并为一个的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Camera框架
- 下一篇: 这届全明星,把NBA又燃回来了