In this short tutorial, you will learn how to delete comments in PDF in Java. All the required information is provided for setting the IDE, a list of steps for writing the minimal application to perform the task accompanied by a runnable sample code to remove comments from PDF in Java. This article also discusses other annotations similar to the comments that can be handled while working with PDF files.
Steps to Remove All Comments from PDF in Java
- Set the IDE to use Aspose.PDF for Java to delete comments
- Open the target PDF file using the Document class object for removing comments
- Parse each page of the loaded PDF file and access the collection of annotations on it
- Parse each annotation and filter the Text type annotations for removing them
- Call the delete() method in the Annotations collection to delete the Text annotation
- Save the output PDF file having no Text annotation in it
These steps encompass the process to remove comments in PDF in Java. The process is quite simple where a PDF file is loaded and all the pages are parsed as each page has its own collection of different types of annotations including the Text type annotations that denote the comments in a PDF file. Once the target annotations are detected, those are deleted and the resultant PDF file is saved on the disk.
Code to Delete All Comments from PDF in Java
The aforementioned sample code demonstrates the simple process to delete comments from PDF in Java. The main classes that are used for this process include the Document class for loading the PDF file, the PageCollection to access each Page, the AnnotationCollection class for accessing all the Annotation objects in a Page, and AnnotationType enumerator for checking the Text type annotations whereas it has other types of annotations as well for example underline, strikeout, highlight, link, and watermark to name a few. The delete() method in the AnnotationCollection class contains multiple overloaded functions to delete all or selected annotations as per requirement.
This topic has enlightened us about how to remove comments in PDF in Java. If you are interested in learning the process to insert comments in a PDF, refer to the article on how to insert comment in PDF using Java.