Delete PDF Pages programmatically

You can delete pages from a PDF file using Aspose.PDF for Java. To delete a particular page from the PageCollection simply call the delete() method and specify the index of the particular page you want to delete. Then call the save method to save the updated PDF file.

Delete Page from PDF File

  1. Call the Delete method and specify the page’s index
  2. Call the Save method to save the updated PDF file The following code snippet shows how to delete a particular page from the PDF file using Java.
package com.aspose.pdf.examples;

import com.aspose.pdf.*;

public class ExampleDeletePage {

  private static String _dataDir = "/home/admin1/pdf-examples/Samples/";

  public static void DeletePageFromPDFFile() {

    // 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);    

  }