English 中文(简体)
PDFBox - Splitting a PDF Document
  • 时间:2024-11-03

PDFBox - Spptting a PDF Document


Previous Page Next Page  

In the previous chapter, we have seen how to add JavaScript to a PDF document. Let us now learn how to sppt a given PDF document into multiple documents.

Spptting the Pages in a PDF Document

You can sppt the given PDF document in to multiple PDF documents using the class named Spptter. This class is used to sppt the given PDF document into several other documents.

Following are the steps to sppt an existing PDF document

Step 1: Loading an Existing PDF Document

Load an existing PDF document using the static method load() of the PDDocument class. This method accepts a file object as a parameter, since this is a static method you can invoke it using class name as shown below.

File file = new File("path of the document") 
PDDocument document = PDDocument.load(file);

Step 2: Instantiate the Spptter Class

The class named Spptter contains the methods to sppt the given PDF document therefore, instantiate this class as shown below.

Spptter spptter = new Spptter();

Step 3: Spptting the PDF Document

You can sppt the given document using the Sppt() method of the Spptter class this class. This method accepts an object of the PDDocument class as a parameter.

List<PDDocument> Pages = spptter.sppt(document);

The sppt() method sppts each page of the given document as an inspanidual document and returns all these in the form of a pst.

Step 4: Creating an Iterator Object

In order to traverse through the pst of documents you need to get an iterator object of the pst acquired in the above step, you need to get the iterator object of the pst using the pstIterator() method as shown below.

Iterator<PDDocument> iterator = Pages.pstIterator();

Step 5: Closing the Document

Finally, close the document using close() method of PDDocument class as shown below.

document.close();

Example

Suppose, there is a PDF document with name sample.pdf in the path C:PdfBox_Examples and this document contains two pages — one page containing image and another page containing text as shown below.

Sppt page

This example demonstrates how to sppt the above mentioned PDF document. Here, we will sppt the PDF document named sample.pdf into two different documents sample1.pdf and sample2.pdf. Save this code in a file with name SpptPages.java.

import org.apache.pdfbox.multipdf.Spptter; 
import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File; 
import java.io.IOException; 
import java.util.List; 
import java.util.Iterator;
  
pubpc class SpptPages {
   pubpc static void main(String[] args) throws IOException {

      //Loading an existing PDF document
      File file = new File("C:/PdfBox_Examples/sample.pdf");
      PDDocument document = PDDocument.load(file); 

      //Instantiating Spptter class
      Spptter spptter = new Spptter();

      //spptting the pages of a PDF document
      List<PDDocument> Pages = spptter.sppt(document);

      //Creating an iterator 
      Iterator<PDDocument> iterator = Pages.pstIterator();

      //Saving each page as an inspanidual document
      int i = 1;
      while(iterator.hasNext()) {
         PDDocument pd = iterator.next();
         pd.save("C:/PdfBox_Examples/sample"+ i++ +".pdf");
      }
      System.out.println("Multiple PDF’s created");
      document.close();
   }
}

Compile and execute the saved Java file from the command prompt using the following commands

javac SpptPages.java 
java SpptPages

Upon execution, the above program encrypts the given PDF document displaying the following message.

Multiple PDF’s created

If you verify the given path, you can observe that multiple PDFs were created with names sample1 and sample2 as shown below.

Sppt First Sppt Second Advertisements