天天看點

使用PDFBox合并多個pdf檔案

使用PDFBox合并多個pdf檔案
public class MergePDFs {
   public static void main(String[] args) throws IOException {
      //Loading an existing PDF document
      File file1 = new File("C:/PdfBox_Examples/sample1.pdf");
      PDDocument doc1 = PDDocument.<i>load</i>(file1);
      File file2 = new File("C:/PdfBox_Examples/sample2.pdf");
      PDDocument doc2 = PDDocument.<i>load</i>(file2);
      //Instantiating PDFMergerUtility class
      PDFMergerUtility PDFmerger = new PDFMergerUtility();
      //Setting the destination file
      PDFmerger.setDestinationFileName("C:/PdfBox_Examples/merged.pdf");
      //adding the source files
      PDFmerger.addSource(file1);
      PDFmerger.addSource(file2);
      //Merging the two documents
      PDFmerger.mergeDocuments();
      System.out.println("Documents merged");
      //Closing the documents
      doc1.close();
      doc2.close();
   }
}
           
https://iowiki.com/pdfbox/pdfbox_merging_multiple_pdf_documents.html

官方文檔