生活随笔
收集整理的這篇文章主要介紹了
Java多个pdf文件合并一个pdf(多页)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
直接Ctrl C/V可用
Java代碼
import java
.io
.FileOutputStream
;
import com
.lowagie
.text
.Document
;
import com
.lowagie
.text
.pdf
.PdfCopy
;
import com
.lowagie
.text
.pdf
.PdfImportedPage
;
import com
.lowagie
.text
.pdf
.PdfReader
;
public class MergePdfFile { public static void main(String
[] args
) { String
[] files
= { "C:/Users/Administrator/Desktop/4.pdf", "C:/Users/Administrator/Desktop/5.pdf","C:/Users/Administrator/Desktop/6.pdf"}; String savepath
= "C:/Users/Administrator/Desktop/00.pdf"; mergePdfFiles(files
, savepath
); }public static String
mergePdfFiles(String
[] files
, String newfile
) { Document document
= null
; try { document
= new Document(new PdfReader(files
[0]).getPageSize(1)); PdfCopy copy
= new PdfCopy(document
, new FileOutputStream(newfile
)); document
.open(); for (int i
= 0; i
< files
.length
; i
++) { PdfReader reader
= new PdfReader(files
[i
]); int n
= reader
.getNumberOfPages(); for (int j
= 1; j
<= n
; j
++) { document
.newPage(); PdfImportedPage page
= copy
.getImportedPage(reader
, j
); copy
.addPage(page
); }} } catch (Exception e
) { e
.printStackTrace(); } finally { document
.close(); } return newfile
; }
}
總結(jié)
以上是生活随笔為你收集整理的Java多个pdf文件合并一个pdf(多页)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。