Delete Pages from PDF

Contents
[ ]

Sometimes PDF documents contain unnecessary or sensitive pages that need to be removed. Using Aspose.PDF for Python, developers can programmatically delete specific pages from a PDF without manually editing the file.

Our example shows how to use the delete method of the PdfFileEditor class to remove pages from a PDF document. By specifying the page numbers to delete, you can create a new PDF that excludes unwanted pages. This functionality is useful for cleaning up reports, removing confidential information, or preparing custom document extracts.

  1. Create a PdfFileEditor Object.
  2. Define Pages to Delete.
  3. Delete the Pages.

    import aspose.pdf as ap
    import aspose.pdf.facades as pdf_facades

    import sys
    from os import path

    sys.path.append(path.join(path.dirname(__file__), ".."))
    from config import set_license, initialize_data_dir


    # Delete Pages from PDF
    def delete_pages_from_pdf(infile, outfile):
        # Create a PdfFileEditor object
        pdf_editor = pdf_facades.PdfFileEditor()

        # Define the page numbers to be deleted (1-based index)
        pages_to_delete = [2, 4]

        # Delete the specified pages from the PDF document
        pdf_editor.delete(infile, pages_to_delete, outfile)