使用PDFBox合并多个pdf文件

逃离我推掉我的手 2023-01-03 14:30 330阅读 0赞

使用PDFBox合并多个pdf文件

  1. public class MergePDFs {
  2. public static void main(String[] args) throws IOException {
  3. //Loading an existing PDF document
  4. File file1 = new File("C:/PdfBox_Examples/sample1.pdf");
  5. PDDocument doc1 = PDDocument.<i>load</i>(file1);
  6. File file2 = new File("C:/PdfBox_Examples/sample2.pdf");
  7. PDDocument doc2 = PDDocument.<i>load</i>(file2);
  8. //Instantiating PDFMergerUtility class
  9. PDFMergerUtility PDFmerger = new PDFMergerUtility();
  10. //Setting the destination file
  11. PDFmerger.setDestinationFileName("C:/PdfBox_Examples/merged.pdf");
  12. //adding the source files
  13. PDFmerger.addSource(file1);
  14. PDFmerger.addSource(file2);
  15. //Merging the two documents
  16. PDFmerger.mergeDocuments();
  17. System.out.println("Documents merged");
  18. //Closing the documents
  19. doc1.close();
  20. doc2.close();
  21. }
  22. }

https://iowiki.com/pdfbox/pdfbox_merging_multiple_pdf_documents.html官方文档

发表评论

表情:
评论列表 (有 0 条评论,330人围观)

还没有评论,来说两句吧...

相关阅读