Define annotation behavior with flags on Android
Each Annotation supports AnnotationFlags that define its behavior and capabilities. Access these flags through the flags property. In Java, use getFlags() and setFlags().
Capabilities of flags
The PDF specification defines annotation flags. These flags control annotation behavior, onscreen and printed appearance, and available editing features. Use flags for tasks such as these:
- Set
HIDDENto hide an annotation onscreen and exclude it from printing. - Set
NOVIEWandPRINTto print an annotation without displaying it onscreen. - Set
LOCKEDandLOCKEDCONTENTSto prevent users from editing an annotation.
For the complete list of flags, refer to the AnnotationFlags API reference.
Usage example
The following example creates a locked annotation that users can’t edit:
// Create a new annotation.val annotation: Annotation = FreeTextAnnotation(0, RectF(100f, 140f, 300f, 100f), "Locked text")
// Lock the annotation using flags.annotation.flags = EnumSet.of( AnnotationFlags.LOCKED, AnnotationFlags.LOCKEDCONTENTS)
// Add the newly created annotation to the document.lifecycleScope.launch { document.annotationProvider.addAnnotationToPage(annotation)}import kotlin.coroutines.EmptyCoroutineContext;import kotlinx.coroutines.BuildersKt;
// Create a new annotation.final Annotation annotation = new FreeTextAnnotation(0, new RectF(100, 140, 300, 100), "Locked text");
// Lock the annotation using flags.annotation.setFlags(EnumSet.of( AnnotationFlags.LOCKED, AnnotationFlags.LOCKEDCONTENTS));
// Add the newly created annotation to the document.// `addAnnotationToPage()` is a suspend function. `runBlocking()` waits for it to finish,// so only call this on a worker thread.try { BuildersKt.runBlocking( EmptyCoroutineContext.INSTANCE, (scope, continuation) -> document.getAnnotationProvider().addAnnotationToPage(annotation, continuation));} catch (InterruptedException e) { Thread.currentThread().interrupt();}