Remove Tables from existing PDF

In order to remove the tables, we need to use TableAbsorber class to get hold of tables in existing PDF and then call Remove method.

Remove Table from PDF document

We have added new function i.e. Remove() to the existing TableAbsorber Class in order to remove table from PDF document. Once the absorber successfully finds tables on the page, it becomes capable to remove them. Please check following code snippet showing how to remove a table from PDF document:

package com.aspose.pdf.examples;

import com.aspose.pdf.*;

public class ExampleRemoveTable {
    
    private static String _dataDir = "/home/admin1/pdf-examples/Samples/";

    public static void RemoveTable() {
        // Load existing PDF document
        Document pdfDocument = new Document(_dataDir + "Table_input.pdf");

        // Create TableAbsorber object to find tables
        TableAbsorber absorber = new TableAbsorber();

        // Visit first page with absorber
        absorber.visit(pdfDocument.getPages().get_Item(1));

        // Get first table on the page
        AbsorbedTable table = absorber.getTableList().get(0);

        // Remove the table
        absorber.remove(table);

        // Save PDF
        pdfDocument.save(_dataDir + "Table_out.pdf");
    }  

Remove Multiple Tables from PDF document

Sometimes a PDF document may contain more than one table and you may come up with a requirement to remove multiple tables from it. In order to remove multiple tables from PDF document, please use the following code snippet:

    public static void RemoveMultipleTable() {
        // Load existing PDF document
        Document pdfDocument = new Document(_dataDir + "Table_input2.pdf");

        // Create TableAbsorber object to find tables
        TableAbsorber absorber = new TableAbsorber();

        // Visit second page with absorber
        absorber.visit(pdfDocument.getPages().get_Item(2));

        // Loop through the copy of collection and removing tables
        for (AbsorbedTable table : absorber.getTableList())
            absorber.remove(table);

        // Save document
        pdfDocument.save(_dataDir + "Table2_out.pdf");
    }
}