This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/android/annotations/programmatically-creating-annotations.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Programmatically create PDF annotations on Android | Nutrient

Nutrient supports programmatic creation of link, highlight, free text, ink, note, and other annotation types.

To create an annotation programmatically, follow these steps:

  1. Initialize the annotation.
  2. Set the annotation’s properties.
  3. Add the annotation to the document with PdfFragment#addAnnotationToPage().

For supported annotation types, refer to the introduction to annotations guide.

Examples of annotation creation

The Catalog app’s AnnotationCreationExample demonstrates how to add annotations programmatically. The following examples run inside a PdfActivity.

Outside a PdfActivity, replace getPdfFragment() with a reference to your PdfFragment.

Use LinkAnnotation to create a link to another page:

// Create the link on the first page.
val linkAnnotation = LinkAnnotation(0)
// Set the position.
linkAnnotation.boundingBox = RectF(100f, 100f, 300f, 140f)
// Set where the link points to — in this case, the fifth page.
linkAnnotation.action = GoToAction(4)
// Add it to the page.
pdfFragment?.addAnnotationToPage(linkAnnotation, false)

Highlight annotations

Use HighlightAnnotation to highlight text:

// Define the places you want to highlight.
val textRects = listOf(RectF(100f, 100f, 300f, 140f))
// Create the highlight on the first page.
val highlightAnnotation = HighlightAnnotation(0, textRects)
// Set the color.
highlightAnnotation.color = Color.RED
// Add it to the page.
pdfFragment?.addAnnotationToPage(highlightAnnotation, false)

Free text annotations

Use FreeTextAnnotation to add text to a page:

// Create the text on the first page.
val freeTextAnnotation = FreeTextAnnotation(0, RectF(100f, 100f, 300f, 140f), "My Annotation")
// Set the text color.
freeTextAnnotation.color = Color.RED
// Set the text size.
freeTextAnnotation.textSize = 20f
// Add it to the page.
pdfFragment?.addAnnotationToPage(freeTextAnnotation, false)

Ink annotations

Use InkAnnotation to create lines from a list of points:

// Create the ink annotation on the first page.
val inkAnnotation = InkAnnotation(0)
// Set the line color.
inkAnnotation.color = Color.RED
// Set the line width.
inkAnnotation.lineWidth = 10f
// Create a line from a list of points.
val line = listOf(
PointF(100f, 100f),
PointF(150f, 150f),
PointF(200f, 100f),
PointF(250f, 150f),
PointF(200f, 200f)
)
// Ink annotations can hold multiple lines. We only use one here.
inkAnnotation.lines = listOf(line)
// Add it to the page.
pdfFragment?.addAnnotationToPage(inkAnnotation, false)

Note annotations

Use NoteAnnotation to add a note to a page:

// Create the note on the first page.
// The note annotation is rendered as a fixed size, much like how Adobe Acrobat renders it.
// By default, Nutrient renders note annotations at a fixed size of 32x32pt. We recommend that you set the `boundingBox` to the same value.
val noteAnnotation = NoteAnnotation(0,
RectF(100f, 100f, 132f, 132f),
"This is my note",
NoteAnnotation.NOTE)
// Change the icon.
noteAnnotation.iconName = NoteAnnotation.NEW_PARAGRAPH
// Add it to the page.
pdfFragment?.addAnnotationToPage(noteAnnotation, false)

Stamp annotations

Use StampAnnotation to add a stamp to a page:

// Create the stamp on the first page.
val stampAnnotation = StampAnnotation(0,
RectF(100f, 100f, 300f, 140f),
StampType.CONFIDENTIAL)
// Add it to the page.
pdfFragment?.addAnnotationToPage(stampAnnotation, false)

File annotations with embedded files

Use FileAnnotation with EmbeddedFileSource to attach an embedded file to a page:

// Create the file source with the file you want to embed.
val embeddedFileSource = EmbeddedFileSource(AssetDataProvider("Guide-v5.pdf"),
"Guide-v5.pdf",
"A guide.")
// Create the file annotation on the first page.
val fileAnnotation = FileAnnotation(0,
RectF(100f, 100f, 132f, 132f),
embeddedFileSource)
// Change the icon.
fileAnnotation.setIconName(FileAnnotation.PAPERCLIP)
// Change the color.
fileAnnotation.color = Color.RED
// Add it to the page.
pdfFragment?.addAnnotationToPage(fileAnnotation, false)