Delete PDF Pages in Java
Contents
[
Hide
]
Use the document page collection when you need to remove one or more pages from a PDF.
Delete a single page
Use this example when you need to remove one page by its index.
- Open the source PDF Document.
- Delete the target page from the page collection.
- Save the updated document.
public static void deletePage(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
document.getPages().delete(2);
document.save(outputFile.toString());
}
}
Delete multiple pages
Use this example when several pages should be removed in one operation.
- Open the source PDF Document.
- Pass the page indexes to delete from the page collection.
- Save the modified PDF.
public static void deleteBunchPages(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
document.getPages().delete(new Integer[]{2, 3, 4});
document.save(outputFile.toString());
}
}