如何使用Java将PDF拆分为多个。(How to split a PDF in to many using Java.)

优质
小牛编辑
130浏览
2023-12-01

问题描述 (Problem Description)

如何使用Java将PDF拆分为多个。

解决方案 (Solution)

以下是使用Java将PDF拆分为多个的示例程序。

import org.apache.pdfbox.multipdf.Splitter; 
import org.apache.pdfbox.pdmodel.PDDocument;  
import java.io.File; 
import java.io.IOException; 
import java.util.List; 
import java.util.Iterator;  
public class SplittingPDF { 
   public static void main(String[] args) throws IOException { 
      //Loading an existing PDF document 
      File file = new File("C:/pdfBox/splitpdf_IP.pdf"); 
      PDDocument doc = PDDocument.load(file); 
      //Instantiating Splitter class 
      Splitter splitter = new Splitter(); 
      //splitting the pages of a PDF document 
      List<PDDocument> Pages = splitter.split(doc); 
      //Creating an iterator 
      Iterator<PDDocument> iterator = Pages.listIterator();         
      //Saving each page as an individual document 
      int i = 1; 
      while(iterator.hasNext()){ 
         PDDocument pd = iterator.next(); 
         pd.save("C:/pdfBox/splitOP"+ i++ +".pdf");             
      } 
      System.out.println("PDF splitted");     
   } 
}

输入 (Input)

拆分输入

输出 (Output)

拆分输出

拆分输出