Java實現PDF的拆分
阿新 • • 發佈:2022-05-06
Maven依賴
<!-- PDF拆分與合併工具-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.22</version>
</dependency>
測試程式碼
package com; import java.io.File; import java.io.IOException; import java.util.List; import org.apache.pdfbox.multipdf.Splitter; import org.apache.pdfbox.pdmodel.PDDocument; public class PDFSplit { public static void main(String[] args) throws IOException { Splitter splitter = new Splitter(); // 設定起始頁、結束頁,每個檔案的頁數 splitter.setStartPage(5); splitter.setEndPage(6); splitter.setSplitAtPage(2); PDDocument source = PDDocument.load(new File("C:/Users/Administrator/Desktop/檔名")); List<PDDocument> list = splitter.split(source); int i = 0; for (PDDocument document : list) { i++; document.save("C:/Users/Administrator/Desktop/" + "out" + i + ".pdf"); } } }