Skip to content
DemoSampleAPI ReferenceFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Android Guides Edit annotations

Edit Annotations ​

For operations across pages, see Copy and Share Annotations. To display a badge in the reader without changing the PDF appearance, see Annotation Overlay.

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()
  }
}

Set Annotation Flags ​

Starting in Android SDK 3.2.1, use addFlags(), removeFlags(), and hasFlags() to update or check individual bits in CPDFAnnotation.Flags. hasFlags() returns true only if all requested bits are set. setFlags() replaces the entire mask; use the individual flag operations when you need to preserve the other bits.

For example, set Locked to prevent users from changing the position and appearance of a regular annotation without changing its other flags. Here, annotation is the selected annotation and pageView is the CPDFPageView displaying it. Run the update on the main thread:

java
int locked = CPDFAnnotation.Flags.PDFAnnotationFlagLocked;
if (annotation.addFlags(locked)) {
    pageView.onAnnotationFlagsChanged(annotation);
}

To unlock it, call annotation.removeFlags(locked) and notify pageView again if the call succeeds. Locked does not prevent content editing on its own. Removing it does not clear any existing read-only or content-lock flag.

FlagReader behavior
Hidden, NoViewDoes not draw the annotation overlay or allow selection.
ReadOnlyPrevents selection, moving, deletion, and editing in the UI. Read-only links can still navigate to their targets.
LockedAllows selection and content editing, but prevents moving, resizing, deletion, and property changes.
LockedContentsPrevents content editing but does not restrict moving or deletion.
PrintUse setPrint() and isPrint() to write and read the print flag. It does not affect on-screen display.
Invisible, NoZoom, NoRotate, ToggleNoViewCan be read, set, and saved in the PDF, but do not change how the reader displays or handles the annotation.

These rules apply to user actions in the reader. They do not restrict direct SDK calls or replace document permissions. Form controls use the field's read-only property. XFDF does not preserve LockedContents; call setLockedContents(true) after import if needed.

Save Custom Data ​

Use CPDFAnnotation.setCustomData() to store app-defined JSON, such as a review status or a record ID. The data is saved in the PDF without changing the annotation's appearance, and no overlay is required. In this example, annotation was retrieved from document as described in Access Annotations.

java
JSONObject data = new JSONObject();
data.put("status", "pending");
if (annotation.setCustomData(data)) {
    document.save();
}

After reopening the saved PDF, retrieve the same annotation and read its status:

java
JSONObject stored = reopenedAnnotation.getCustomData();
String status = stored == null ? "" : stored.optString("status");

To clear the data, call setCustomData(null) and save the document. getCustomData() returns an independent copy; after modifying it, pass it back to setCustomData() to store the changes. Do not use setExtensionInfo() to update custom JSON, since it replaces the entire extension information field.