Remove Pages from PDF via Java

Delete pages from PDF document using Aspose.PDF for Java

How to Delete pages from PDF Using Java

Remove pages from PDF using the Aspose.PDF for Java. To protect private information, you may need to delete pages or sections containing confidential data before sharing the document with others. Large PDF files can be cumbersome to share or store. Deleting pages helps reduce the file size, making it easier to transmit or archive. Also, removing pages streamline the document and deletes unnecessary repetition. A document may contain pages with errors or outdated information. Deleting these pages enhances the document’s overall quality. Removing pages can make it easier for users to navigate a PDF, ensuring they find the information they need quickly without scrolling through irrelevant content. When deleting pages from a PDF, it’s important to use a reliable PDF editor or software to ensure that the resulting document remains accurate and error-free. The Aspose.PDF for Java library can remove pages from PDF in a few steps. In order to delete page, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

Delete Page from PDF via Java


You need Aspose.PDF for Java to try the code in your environment.

  1. Open the existing document.

  2. Delete a particular page.

  3. Save the output PDF using the Save method.

The provided Java code snippet shows how to delete a particular page from a PDF document using the Aspose.PDF library. First, you should open an existing PDF document named ‘DeleteParticularPage.pdf’. Next, delete a particular page from the PDF document. Save the updated PDF document to the specified directory with the new file name ‘DeleteParticularPage_out.pdf’.

Remove Pages from PDF

This sample code shows how to delete pages from PDF - Java


    // Open document
    Document pdfDocument = new Document(_dataDir + "sample.pdf");

    // Delete a particular page
    pdfDocument.getPages().delete(2);

    _dataDir = _dataDir + "DeleteParticularPage_out.pdf";
    // Save updated PDF
    pdfDocument.save(_dataDir);