When editing PDF documents, users often need to organize pages to make the documents fit their specific requirements. For instance, they may need to insert new pages to include additional information or graphics, rearrange pages to ensure that the content is presented in a logical order, or delete pages containing confidential or sensitive information to protect privacy. In this article, I will explain how to organize pages in a PDF document in Java.
The following topics will be covered in this article:
- Insert Pages into a PDF Document in Java
- Delete Pages from a PDF Document in Java
- Rearrange Pages in a PDF Document in Java
- Rotate Pages in a PDF Document in Java
- Import Pages from Another PDF Document in Java
- Extract Pages from a PDF Document in Java
Add Dependencies
To organize pages in pdf, this article uses Spire.PDF for Java library. If you are using Maven, you can install the jar of Spire.PDF for Java into your project by adding the following code to your project’s pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.pdf </artifactId>
<version>9.3.11</version>
</dependency>
</dependencies>
If you are not using Maven, you can download Spire.PDF for Java from the official website, extract the package and then import the Spire.Pdf.jar under the lib folder into your project as a dependency.
Insert Pages into a PDF Document in Java
You can insert a page at a specific position of a PDF document using the PdfDocument.getPages().insert(int index) method or add a page to the end of a PDF document using the PdfDocument.getPages().add() method.
The following are the detailed steps:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.loadFromFile() method.
- Insert a page at a specific position of the document using the PdfDocument.getPages().insert(int index) method, or add a page to the end of a PDF document using the PdfDocument.getPages().add() method.
- Save the result document using the PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
public class InsertPages {
public static void main(String[] args) {
//Initialize an instance of the PdfDocument class
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.loadFromFile("Sample.Pdf");
//Insert a page at the first index position
pdf.getPages().insert(0);
//Add a page to the end of the document
//pdf.getPages().add();
//Save the result document
pdf.saveToFile("InsertPages.pdf");
}
}
Delete Pages from a PDF Document in Java
You can delete a specific page from a PDF document using the PdfDocument.getPages().removeAt(int index) method.
The following are the detailed steps:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.loadFromFile() method.
- Delete a specific page by its index using the PdfDocument.getPages().removeAt(int index) method.
- Save the result document using the PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
public class DeletePages {
public static void main(String[] args) {
//Initialize an instance of the PdfDocument class
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.loadFromFile("Sample.Pdf");
//Delete the first page by page index
pdf.getPages().removeAt(0);
//Save the result document
pdf.saveToFile("DeletePages.pdf");
}
}
Rearrange Pages in a PDF Document in Java
You can rearrange the pages in a PDF document using the PdfDocument.getPages().reArrange(int[] orderArray) method.
The following are the detailed steps:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.loadFromFile() method.
- Create an int array for the new page order.
- Rearrange the pages in the document according to the order specified in the int array using the PdfDocument.getPages().reArrange(int[] orderArray) method.
- Save the result document using the PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
public class RearrangePages {
public static void main(String[] args) {
//Initialize an instance of the PdfDocument class
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.loadFromFile("Sample.Pdf");
//Create an int array for new page order
//Original page order: 0, 1, 2, 3. Changed page order: 1, 0, 2, 3
int[] orderArray = new int[] { 1, 0, 2, 3 };
//Rearrange the pages according to the order specified in the array
pdf.getPages().reArrange(orderArray);
//Save the result document
pdf.saveToFile("RearrangePages.pdf");
}
}
Rotate Pages in a PDF Document in Java
You can rotate a PDF page using the PdfPageBase.setRotation(PdfPageRotateAngle rotateAngle) method.
The following are the detailed steps:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.loadFromFile() method.
- Get the page that you want to rotate using the PdfDocument.getPages().get(int index) method.
- Get the original rotation angle of the page using the PdfPageBase.getRotation().getValue() method.
- Increase the original rotation angle by desired degrees.
- Apply the new rotation angle to the page using the PdfPageBase.setRotation(PdfPageRotateAngle rotateAngle) method.
- Save the result document using the PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageRotateAngle;
public class RotatePages {
public static void main(String[] args) {
//Initialize an instance of the PdfDocument class
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.loadFromFile("Sample.pdf");
//Get the first page
PdfPageBase page = pdf.getPages().get(0);
//Get the original rotation angle of the page
int rotation = (int)page.getRotation().getValue();
//Rotate the page 180 degrees clockwise based on the original rotation angle
rotation += PdfPageRotateAngle.Rotate_Angle_180.getValue();
page.setRotation(PdfPageRotateAngle.fromValue(rotation));
//Save the result document
pdf.saveToFile("RotatePages.pdf");
}
}
Import Pages from Another PDF Document in Java
You can import a page or a range of pages from one PDF document to another PDF document using the PdfDocument.insertPage(PdfDocument doc, int pageIndex) or PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) method.
The detailed steps are as follows:
- Initialize an instance of the PdfDocument class and load the first PDF document using the PdfDocument.loadFromFile() method.
- Initialize an instance of the PdfDocument class and load the second PDF document using the PdfDocument.loadFromFile() method.
- Import a specific page or a range of pages from the second PDF document to the first PDF document using the PdfDocument.insertPage(PdfDocument doc, int pageIndex) or PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) method.
- Save the first PDF document to file using the PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
public class ImportPages {
public static void main(String[] args) {
//Initialize an instance of the PdfDocument class
PdfDocument pdf1 = new PdfDocument();
//Load the first PDF document
pdf1.loadFromFile("Sample.Pdf");
//Initialize an instance of the PdfDocument class
PdfDocument pdf2 = new PdfDocument();
//Load the second PDF document
pdf2.loadFromFile("test.Pdf");
//Import the first page of the second PDF to the first PDF
pdf1.insertPage(pdf2, 0);
//Import a range of pages (page 1 - 4) of the second PDF to the first PDF
//pdf1.insertPageRange(pdf2, 0, 3);
//Save the first PDF document to file
pdf1.saveToFile("ImportPages.pdf");
}
}
Extract Pages from a PDF Document in Java
The PdfDocument.insertPage(PdfDocument doc, int pageIndex) and PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) methods can also be used to extract a specific page or a range of pages from a PDF document to a new PDF document.
The detailed steps are as follows:
- Initialize an instance of the PdfDocument class and load the source PDF document using the PdfDocument.loadFromFile() method.
- Initialize an instance of the PdfDocument class to create a new PDF document.
- Extract a specific page or a range of pages from the source PDF document to the new PDF document using the PdfDocument.insertPage(PdfDocument doc, int pageIndex) or PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) method.
- Save the new PDF document to file using the PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
public class ExtractPages {
public static void main(String[] args) {
//Initialize an instance of the PdfDocument class
PdfDocument pdf = new PdfDocument();
//Load the source PDF document
pdf.loadFromFile("Sample.Pdf");
//Initialize an instance of the PdfDocument class to create a new PDF document
PdfDocument newPdf = new PdfDocument();
//Extract the first page of the source PDF to the new PDF
newPdf.insertPage(pdf, 0);
//Extract a range of pages (page 1 - 4) of the source PDF to the new PDF
//newPdf.insertPageRange(pdf, 0, 3);
//Save the new PDF document to file
newPdf.saveToFile("ExtractPages.pdf");
}
}