Delete Pages from a PDF
Removing PDF Pages with IronPDF
Removing one or more pages from a PDF document is a trivial task with IronPDF.
The featured code example uses the removePages
method to modify a four-page PDF document (generated on the fly for demonstration purposes). The resulting PDF saved to the local filesystem includes only the first and fourth pages of the original document (effectively deleting the middle two pages from the document, as expected).
The PageSelection
class is used to specify the pages that removePages
will delete from a loaded PdfDocument
object. Developers can use the class's static pageRange
method to remove a sequential range of pages (pages 4 - 18 of a fictitious document, for instance). To remove a list of individual pages (not ordered sequentially), provide a List
of page indexes to pageRange
instead:
How to Delete PDF Pages Using Java
- Install IronPDF Java Library
- Import existing PDF file or render new PDF in Java
- Remove pages from PDF with
removePages
method - Specify the pages to be deleted using
PageSelection
class - Save the resulting PDF as a new document
Here is a Java code example demonstrating these steps:
import com.ironpdf.PdfDocument;
import com.ironpdf.PageSelection;
import java.io.IOException;
public class PdfPageRemover {
public static void main(String[] args) {
try {
// Load an existing PDF document
PdfDocument pdf = PdfDocument.load("path/to/input/document.pdf");
// Define the pages to keep. Here we remove pages 2 and 3
PageSelection pageSelection = PageSelection.not(PageSelection.pageRange(2, 3));
// Remove the specified pages from the PdfDocument
pdf.removePages(pageSelection);
// Save the modified PDF to a new file
pdf.saveAs("path/to/output/document.pdf");
System.out.println("PDF pages removed successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import com.ironpdf.PdfDocument;
import com.ironpdf.PageSelection;
import java.io.IOException;
public class PdfPageRemover {
public static void main(String[] args) {
try {
// Load an existing PDF document
PdfDocument pdf = PdfDocument.load("path/to/input/document.pdf");
// Define the pages to keep. Here we remove pages 2 and 3
PageSelection pageSelection = PageSelection.not(PageSelection.pageRange(2, 3));
// Remove the specified pages from the PdfDocument
pdf.removePages(pageSelection);
// Save the modified PDF to a new file
pdf.saveAs("path/to/output/document.pdf");
System.out.println("PDF pages removed successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation of the code:
- Import Statements: Import necessary classes from the IronPDF library to manage PDF documents.
- Loading the PDF: Load an existing PDF document using the static method
PdfDocument.load
. - Defining Page Selection: Use the
PageSelection
class to specify which pages should be removed. In this example, pages 2 and 3 are removed by using thepageRange
method and then using thenot
method to inverse the selection. - Modifying the PDF: Call the
removePages
method with the specifiedpageSelection
to remove the unwanted pages. - Saving the PDF: Save the modified document to a new file path using the
saveAs
method.
This concise yet powerful approach of removing pages from a PDF ensures that documents can be easily manipulated according to every specific need.