Skip to main content
Skip table of contents

How to remove text from a PDF?

Is it possible to remove all text occurrences contained in a specified area (red color rectangle area) of ??a pdf document?

I want to remove what is in the red rectangle.

Remove text marked with red rectangle

I want the text to be removed, not merely covered.

Posted on StackOverflow on jan 12, 2015 by amar

Please take a look at the RemoveContentInRectangle example.

Let's say we have the following page:

Original PDF

Original PDF

Now we want to remove all the text in the rectangle defined by the coordinates: llx = 97, lly = 405, urx = 480, ury = 445] (where ll stands for lower-left and ur stands for upper-right).

We can now use the following code:

public void manipulatePdf(String dest) throws IOException {
    //Load the license file to use cleanup features
    LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/itextkey-multiple-products.xml");
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
    List cleanUpLocations = new ArrayList();
    cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(97, 405, 383, 40), Color.GRAY));
    PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, cleanUpLocations);

As you see, we define a list of PdfCleanUpLocation objects. To this list, we add a PdfCleanUpLocation passing the page number, a Rectangle defining the area we want to clean up, and a color that will show the area where content has been removed.

We then pass this list of PdfCleanUpLocations to the PdfCleanUpTool along with the PdfDocument instance. We invoke the cleanUp() method and when we close the PdfDocument instance, we get the following result:

Text in gray area has been completely removed

Text in gray area has been completely removed

You can inspect this file: you will no longer be able to select any text in the gray area. All the text inside that rectangle has been removed.

Click How to remove text from a PDF? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.