The purpose of this article is to guide you on how to add strikeout annotation in PDF using Java. Strikeout annotations are useful for highlighting specific text or content in a PDF file. Fortunately, with the help of a commonly used annotation library, the procedure to insert strikeout annotation in PDF using Java is straightforward. To achieve this, we have listed the necessary steps that you need to follow.
Steps to Add Strikeout Annotation in PDF Using Java
- Use Maven repository to install GroupDocs.Annotation for Java into your project
- Import the necessary classes to add strikeout annotation in PDF
- Create an Annotator class object by providing the PDF file path to its constructor
- Create objects of the Point structure
- Create an object of StrikeoutAnnotation class and set some properties e.g. position, page number, points, etc.
- Call Annotator.add method and pass StrikeoutAnnotation object to it
- Call Annotator.save method to save the PDF to disk
The above steps illustrate how to install the annotation library on your computer, which will allow you to create strikeout annotation to PDF in Java. No additional software is required to insert strikeout annotations into a PDF file. These instructions are compatible with widely used operating systems such as Windows, macOS, and Linux. The code example below demonstrates how to add a strikeout annotation to a PDF.
Code to Add Strikeout Annotation in PDF Using Java
import com.groupdocs.annotation.Annotator; | |
import com.groupdocs.annotation.licenses.License; | |
import com.groupdocs.annotation.models.Point; | |
import com.groupdocs.annotation.models.annotationmodels.StrikeoutAnnotation; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.List; | |
public class AddStrikeoutAnnotationinPDFUsingJava { | |
public static void main(String[] args) { | |
// Set License to avoid the limitations of Annotation library | |
License license = new License(); | |
license.setLicense("GroupDocs.Annotation.lic"); | |
// Create an instance of Annotator class | |
Annotator annotator = new Annotator("input.pdf"); | |
Point point1 = new Point(80, 730); | |
Point point2 = new Point(240, 730); | |
Point point3 = new Point(80, 650); | |
Point point4 = new Point(240, 650); | |
List<Point> points = new ArrayList<Point>(); | |
points.add(point1); | |
points.add(point2); | |
points.add(point3); | |
points.add(point4); | |
// Create an instance of StrikeoutAnnotation class and set options | |
StrikeoutAnnotation strikeout = new StrikeoutAnnotation(); | |
strikeout.setCreatedOn(Calendar.getInstance().getTime()); | |
strikeout.setFontColor(65535); | |
strikeout.setMessage("This is strikeout annotation"); | |
strikeout.setOpacity(0.7); | |
strikeout.setPageNumber(0); | |
strikeout.setPoints(points); | |
// Add strikeout annotation to Annotator | |
annotator.add(strikeout); | |
// Save the final PDF to disk | |
annotator.save("result.pdf"); | |
} | |
} |
The preceding section provided a detailed description of Java strikeout annotation in PDF, along with a basic code example that only requires a few API calls. After installing the annotation library and updating the input and output file paths, you can seamlessly incorporate the code into your applications. Congratulations, you have successfully added a strikeout annotation to a PDF file using Java.
In previous tutorial, we discussed adding underline annotations to PDF files. To learn more about this topic, please consult our guide on how to add underline annotation into PDF using Java .