Annotation Creation With Alpha
Programmatic creation of annotations with alpha.
/* * Copyright © 2025-2026 PSPDFKit GmbH. All rights reserved. * * The PSPDFKit Sample applications are licensed with a modified BSD license. * Please see License for details. This notice may not be removed from this file. */package com.pspdfkit.catalog.examples.kotlin
import android.content.Contextimport android.graphics.Colorimport android.graphics.PointFimport android.graphics.RectFimport android.net.Uriimport android.view.Menuimport android.view.MenuItemimport android.widget.Toastimport androidx.activity.viewModelsimport androidx.annotation.ColorIntimport androidx.annotation.IntRangeimport androidx.annotation.UiThreadimport com.pspdfkit.annotations.Annotationimport com.pspdfkit.annotations.FreeTextAnnotationimport com.pspdfkit.annotations.HighlightAnnotationimport com.pspdfkit.annotations.InkAnnotationimport com.pspdfkit.annotations.LineAnnotationimport com.pspdfkit.annotations.NoteAnnotationimport com.pspdfkit.annotations.PolygonAnnotationimport com.pspdfkit.annotations.PolylineAnnotationimport com.pspdfkit.annotations.SquareAnnotationimport com.pspdfkit.annotations.StampAnnotationimport com.pspdfkit.catalog.Rimport com.pspdfkit.catalog.SdkExampleimport com.pspdfkit.catalog.tasks.ExtractAssetTaskimport com.pspdfkit.catalog.tasks.ExtractAssetTask.OnDocumentExtractedListenerimport com.pspdfkit.configuration.activity.PdfActivityConfigurationimport com.pspdfkit.document.PdfDocumentimport com.pspdfkit.preferences.PSPDFKitPreferencesimport com.pspdfkit.ui.PdfActivityimport com.pspdfkit.ui.PdfActivityIntentBuilderimport java.io.Fileimport kotlin.getValue
class AnnotationWithAlphaCreationExample(context: Context) : SdkExample(context, R.string.annotationWithAlphaCreationExampleTitle, R.string.annotationWithAlphaCreationExampleDescription) { override fun launchExample( context: Context, configuration: PdfActivityConfiguration.Builder ) { configuration // Turn off saving, so we have the clean original document every time the example is // launched. .autosaveEnabled(false) .build()
// The annotation creator written into newly created annotations. If not set, or set to null // a dialog will be shown when creating an annotation, asking you to enter a name. PSPDFKitPreferences.get(context).setAnnotationCreator("John Doe")
// Extract the document from the assets. The launched activity will add annotations to that // document. ExtractAssetTask.extract( WELCOME_DOC, title, context, OnDocumentExtractedListener { documentFile: File? -> val intent = PdfActivityIntentBuilder.fromUri(context, Uri.fromFile(documentFile)) .configuration(configuration.build()) .activityClass(AnnotationWithAlphaCreationActivity::class.java) .build() context.startActivity(intent) } ) }}
class AnnotationWithAlphaCreationActivity : PdfActivity() {
private val viewModel: AnnotationCreationViewModel by viewModels()
override fun onCreateOptionsMenu(menu: Menu): Boolean { super.onCreateOptionsMenu(menu) menu.add(0, ADD_STAMP_ITEM_ID, 0, "Add Stamp") return true }
override fun onOptionsItemSelected(item: MenuItem): Boolean { if (item.itemId == ADD_STAMP_ITEM_ID) { createStamp() return true } else { return super.onOptionsItemSelected(item) } }
@UiThread override fun onDocumentLoaded(document: PdfDocument) { viewModel.createObjects { val pageIndex = 0 createNoteAnnotation(pageIndex) createHighlightAnnotation(pageIndex, "PSPDFKit", Color.YELLOW) createHighlightAnnotation(pageIndex, "QuickStart", Color.GREEN) createFreeTextAnnotation(pageIndex) createInkAnnotation(pageIndex) createLineAnnotation(pageIndex) createPolyline(pageIndex) createPolygon(pageIndex) createSquareAnnotation(pageIndex) } }
private fun createStamp() { val document = document ?: return val pageIndex = pageIndex if (pageIndex < 0) return
val pageSize = document.getPageSize(pageIndex) val halfWidth = pageSize.width / 2 val halfHeight = pageSize.height / 2 val rect = RectF(halfWidth - 100, halfHeight + 100, halfWidth + 100, halfHeight - 100)
val stamp = StampAnnotation(pageIndex, rect, "STAMP_SUBJECT") val color = Color.rgb(255, 0, 0) stamp.color = color stamp.fillColor = Color.rgb(255, 255, 255) stamp.alpha = ALPHA
addAnnotationToPage(stamp) }
private fun createNoteAnnotation(@IntRange(from = 0) pageIndex: Int) { val pageRect = RectF(180f, 692f, 212f, 660f) val contents = "This is note annotation was created from code." val icon = NoteAnnotation.CROSS val color = Color.GREEN
// Create the annotation, and set its properties. val noteAnnotation = NoteAnnotation(pageIndex, pageRect, contents, icon) noteAnnotation.color = color noteAnnotation.alpha = ALPHA
addAnnotationToPage(noteAnnotation) }
private fun createHighlightAnnotation( @IntRange(from = 0) pageIndex: Int, highlightedText: String, @ColorInt color: Int ) { val document = document // Find the provided text on the current page. val textPosition = document!!.getPageText(pageIndex).indexOf(highlightedText)
if (textPosition >= 0) { // To create a text highlight, extract the rects of the text to highlight and pass them // to the annotation constructor. val textRects = document.getPageTextRects(pageIndex, textPosition, highlightedText.length, true) val highlightAnnotation = HighlightAnnotation(pageIndex, textRects)
highlightAnnotation.color = color highlightAnnotation.alpha = ALPHA
addAnnotationToPage(highlightAnnotation) } else { Toast.makeText(this, "Can't find the text to highlight.", Toast.LENGTH_SHORT) .show() } }
private fun createInkAnnotation(@IntRange(from = 0) pageIndex: Int) { val inkAnnotation = InkAnnotation(pageIndex) inkAnnotation.color = Color.rgb(255, 165, 0) // Orange inkAnnotation.alpha = ALPHA inkAnnotation.lineWidth = 10f
// Create a line from a list of points. val line: MutableList<PointF> = ArrayList() var x = 120 while (x < 720) { val y = if (((x % 120) == 0)) 400 else 350 line.add(PointF(x.toFloat(), y.toFloat())) x += 60 }
// Ink annotations can hold multiple lines. This example only uses a single line. inkAnnotation.lines = listOf<List<PointF>>(line)
addAnnotationToPage(inkAnnotation) }
private fun createLineAnnotation(@IntRange(from = 0) pageIndex: Int) { val lineAnnotation = LineAnnotation(pageIndex, PointF(400f, 100f), PointF(500f, 100f))
lineAnnotation.color = Color.CYAN lineAnnotation.alpha = ALPHA lineAnnotation.lineWidth = 20f
addAnnotationToPage(lineAnnotation) }
private fun createPolyline(@IntRange(from = 0) pageIndex: Int) { val polylineAnnotation = PolylineAnnotation( pageIndex, listOf(PointF(400f, 200f), PointF(500f, 200f), PointF(500f, 150f)) )
polylineAnnotation.alpha = ALPHA polylineAnnotation.color = Color.RED polylineAnnotation.lineWidth = 20f
addAnnotationToPage(polylineAnnotation) }
private fun createPolygon(@IntRange(from = 0) pageIndex: Int) { val polygonAnnotation = PolygonAnnotation( pageIndex, listOf(PointF(600f, 600f), PointF(600f, 500f), PointF(500f, 550f)) )
polygonAnnotation.alpha = ALPHA polygonAnnotation.color = Color.RED polygonAnnotation.fillColor = Color.BLUE polygonAnnotation.lineWidth = 20f
addAnnotationToPage(polygonAnnotation) }
private fun createSquareAnnotation(@IntRange(from = 0) pageIndex: Int) { val squareAnnotation = SquareAnnotation(pageIndex, RectF(0f, 100f, 100f, 0f))
squareAnnotation.color = Color.RED squareAnnotation.fillColor = Color.RED squareAnnotation.alpha = ALPHA
addAnnotationToPage(squareAnnotation) }
private fun createFreeTextAnnotation(@IntRange(from = 0) pageIndex: Int) { val contents = "Add text to pages using FreeTextAnnotations" val pageRect = RectF(100f, 980f, 320f, 930f)
val freeTextAnnotation = FreeTextAnnotation(pageIndex, pageRect, contents) freeTextAnnotation.color = Color.BLUE freeTextAnnotation.alpha = ALPHA freeTextAnnotation.textSize = 20f
addAnnotationToPage(freeTextAnnotation) }
/** Adds annotation to page asynchronously, refreshing rendering in all UI components. */ private fun addAnnotationToPage(annotation: Annotation) { pdfFragment?.addAnnotationToPage(annotation, false) }
companion object { private const val ADD_STAMP_ITEM_ID = 1234 private const val ALPHA = 0.4f }}This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.