- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to remove pages from a PDF document using Java
Problem Description
How to remove pages from a PDF document using Java.
Solution
Following is an example program to remove pages from a PDF document using Java.
import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; public class RemovingPagesFromPdf { public static void main(String args[]) throws IOException { //Loading an existing document File file = new File("C:/pdfBox/RemovePages_IP.pdf"); PDDocument doc = PDDocument.load(file); //Listing the number of existing pages System.out.print(doc.getNumberOfPages()); for(int i = 0; i<5; i++){ //removing the pages doc.removePage(i); } System.out.println("page removed"); //Saving the document doc.save("C:/pdfBox/RemovePages_OP.pdf"); //Closing the document doc.close(); } }
Input
Output
java_apache_pdf_box
Advertisements