Skip to content
Guides

Edit Annotations

The steps to edit an annotation are as follows:

  1. Obtain the page object where the annotation needs to be edited.

  2. Access the list of annotations on that page.

  3. Locate the desired annotation in the annotation list and convert it.

  4. Set the properties to the annotation object.

  5. Update the annotation appearance to display it on the document.

This example shows how to edit annotations:

java
CPDFPage page = document.pageAtIndex(0);
List<CPDFAnnotation> annotations = page.getAnnotations();
if (annotations != null && annotations.size() > 0) {
  CPDFAnnotation annotation = annotations.get(0);
  if (annotation.getType() == CPDFAnnotation.Type.FREETEXT) {
    CPDFFreetextAnnotation freetextAnnotation = (CPDFFreetextAnnotation) annotation;
    CPDFTextAttribute textAttribute = new CPDFTextAttribute(CPDFTextAttribute.FontNameHelper.obtainFontName(
      CPDFTextAttribute.FontNameHelper.FontType.Courier, false, false
    ), 20, Color.YELLOW);
    freetextAnnotation.setFreetextDa(textAttribute);
    freetextAnnotation.updateAp();
  }
}
kotlin
val page = document.pageAtIndex(0)
val annotations = page.annotations
if (annotations != null && annotations.size > 0) {
  val annotation = annotations[0]
  if (annotation.type == CPDFAnnotation.Type.FREETEXT) {
    val freetextAnnotation = annotation as CPDFFreetextAnnotation
    freetextAnnotation.freetextDa = CPDFTextAttribute(
      CPDFTextAttribute.FontNameHelper.obtainFontName(
        CPDFTextAttribute.FontNameHelper.FontType.Courier, false, false
      ), 20F, Color.YELLOW
    )
    freetextAnnotation.updateAp()
  }
}