生活随笔
收集整理的这篇文章主要介绍了
Java多个pdf文件合并一个pdf(多页)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
直接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
; }
}
总结
以上是生活随笔为你收集整理的Java多个pdf文件合并一个pdf(多页)的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。