How to Strike Out Text in PDF using Java

This brief tutorial describes how to strike out text in PDF using Java. It contains all the details to configure the environment and provides a running sample code to strike out text in Adobe PDF using Java. You will learn the process to load a PDF and strike out a particular instance of a text or all instances in the loaded PDF file.

Steps to Strike Out Text in Adobe PDF using Java

  1. Set the IDE to add Aspose.PDF for Java to strike out a text
  2. Load the target PDF file using the Document class object and get access to the target page for striking out text
  3. Create a TextFragmentAbsorber object for searching a text
  4. Parse the PDF document to search all the instances of the given text and save them in a collection
  5. Create a StrikeOutAnnotation object to strike out a particular instance of the text found in the document
  6. Set the annotation color and title
  7. Save the resultant PDF file on the disk having strike-out text in it

These steps explain the process of how to strike out text in Adobe PDF using Java. All the necessary classes, methods, and properties are introduced that are required to accomplish the task like the Document class is used to load the PDF file, the TextFragmentAbsorber class object is used to define and search a text in the PDF file, a StrikeOutAnnotation object is used to define the strike out annotation and apply it on a particular instance of the text. The customization of the strike-out annotation is also described in these steps.

Code to Strike Out Text in PDF using Java

This code demonstrates how to strike out in Adobe Acrobat using Java. It initializes the TextFragmentAbsorber object by providing the string data that is to be searched on a particular page in the PDF file. Once the collection of searched text instances is ready, a StrikeOutAnnotation object is created by providing the target PDF page and the rectangle for the selected instance of the text to be struck out. In this way, you may either strike out only the selected instances of the search text or parse through the entire collection and strike out all the instances.

This article has taught us an interesting feature to strike out automatically a particular text on a single page or all the pages of a PDF file. If you want to learn the process to find and replace text in PDF, refer to the article on how to find and replace text in PDF using Java.

 English