在Java编程中,如何使用Java将PDF分割成多个文档?
以下是使用Java将PDF文档页面分割成多个文档的示例程序。
package com.yiibai;
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
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 {
// Creating PDF document object
PDDocument document = new PDDocument();
File file = new File("F:/worksp/javaexamples/java_apache_pdf_box/AddPages.pdf");
PDDocument.load(file);
for (int i = 0; i < 10; i++) {
// Creating a blank page
PDPage blankPage = new PDPage();
// Adding the blank page to the document
document.addPage(blankPage);
}
// Saving the document
document.save("F:/worksp/javaexamples/java_apache_pdf_box/splitpdf_IP.pdf");
System.out.println("PDF created");
// Loading an existing PDF document
File file2 = new File("F:/worksp/javaexamples/java_apache_pdf_box/splitpdf_IP.pdf");
PDDocument doc = PDDocument.load(file2);
// 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("F:/worksp/javaexamples/java_apache_pdf_box/splitOP" + i++ + ".pdf");
}
System.out.println("PDF splitted");
}
}
执行上面示例代码,得到以下结果 -
PDF created
PDF splitted
此时,程序已经分割成多个PDF文件,如下所示 -