Programmatically create annotations on Android
Nutrient supports programmatic creation of link, highlight, free text, ink, note, and other annotation types.
To create an annotation programmatically, follow these steps:
- Initialize the annotation.
- Set the annotation’s properties.
- 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.
Link annotations
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)// Create the link on the first page.final LinkAnnotation linkAnnotation = new LinkAnnotation(0);// Set the position.linkAnnotation.setBoundingBox(new RectF(100, 100, 300, 140));// Set where the link points to — in this case, the fifth page.linkAnnotation.setAction(new GoToAction(4));// Add it to the page.getPdfFragment().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)// Define the places you want to highlight.final List<RectF> textRects = Collections.singletonList(new RectF(100, 100, 300, 140));// Create the highlight on the first page.final HighlightAnnotation highlightAnnotation = new HighlightAnnotation(0, textRects);// Set the color.highlightAnnotation.setColor(Color.RED);// Add it to the page.getPdfFragment().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)// Create the text on the first page.final FreeTextAnnotation freeTextAnnotation = new FreeTextAnnotation(0, new RectF(100, 100, 300, 140), "My Annotation");// Set the text color.freeTextAnnotation.setColor(Color.RED);// Set the text size.freeTextAnnotation.setTextSize(20f);// Add it to the page.getPdfFragment().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)// Create the ink annotation on the first page.final InkAnnotation inkAnnotation = new InkAnnotation(0);// Set the line color.inkAnnotation.setColor(Color.RED);// Set the line width.inkAnnotation.setLineWidth(10f);// Create a line from a list of points.final List<PointF> line = Arrays.asList( new PointF(100, 100), new PointF(150, 150), new PointF(200, 100), new PointF(250, 150), new PointF(200, 200));// Ink annotations can hold multiple lines. We only use one here.inkAnnotation.setLines(Collections.singletonList(line));// Add it to the page.getPdfFragment().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)// 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.final NoteAnnotation noteAnnotation = new NoteAnnotation(0, new RectF(100, 100, 132, 132), "This is my note", NoteAnnotation.NOTE);// Change the icon.noteAnnotation.setIconName(NoteAnnotation.NEW_PARAGRAPH);// Add it to the page.getPdfFragment().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)// Create the stamp on the first page.final StampAnnotation stampAnnotation = new StampAnnotation(0, new RectF(100, 100, 300, 140), StampType.CONFIDENTIAL);// Add it to the page.getPdfFragment().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)// Create the file source with the file you want to embed.EmbeddedFileSource embeddedFileSource = new EmbeddedFileSource(new AssetDataProvider("Guide-v5.pdf"), "Guide-v5.pdf", "A guide.");// Create the file annotation on the first page.final FileAnnotation fileAnnotation = new FileAnnotation(0, new RectF(100, 100, 132, 132), embeddedFileSource);// Change the icon.fileAnnotation.setIconName(FileAnnotation.PAPERCLIP);// Change the color.fileAnnotation.setColor(Color.RED);// Add it to the page.getPdfFragment().addAnnotationToPage(fileAnnotation, false);