PDF Annotations#

Annotations#

Annotations includes functionality for drawing, text markup and placing objects on a PDF document.

App Kit provides the following sets:

Note

You should not set annotation modes on a document until the document has loaded, therefore ensure to add any API calls for annotation methods after listening for the document loaded event.

Document Drawing Annotations#

Document text annotations depend on a “Drawing Annotation Mode” being activated. The following API allows for control over setting this mode as well as finding out what mode is currently active.

Draw Mode#

Turning on drawing is as simple as calling the setDrawModeOn() method against your DocumentView instance. To turn off drawing just call the setDrawModeOff() method against your DocumentView instance.

import com.artifex.sonui.editor.DocumentView

fun setDocumentDrawMode(documentView:DocumentView, enable:Boolean) {
    if (enable) {
        documentView.setDrawModeOn()
    } else {
        documentView.setDrawModeOff()
    }
}

To test if “Draw Mode” is active you can use isDrawModeOn

val dm:Boolean = documentView.isDrawModeOn

When Draw Mode is enabled, the user can draw an ink annotation with the selected line thickness and color. When Draw Mode is then disabled, the annotation is saved to the document.

Drawing Mode Annotation Types#

The simplist drawing annotation is freehand ink drawing. As this was the first supported drawing annotation, in the Android App Kit SDK this is the default implementation. Therefore calling setDrawModeOn() without any parameter will enable ink annotations.

For other drawing types use the following with the setDrawModeOn method:

Drawing mode enumeration#

Mode

Description

DocView.AnnotMode.NONE

None

DocView.AnnotMode.INK

Freehand ink

DocView.AnnotMode.LINE

Line

DocView.AnnotMode.RECTANGLE

Rectangle

DocView.AnnotMode.OVAL

Oval

DocView.AnnotMode.POLYGON

Polygon

DocView.AnnotMode.POLYLINE

Polyline

DocView.AnnotMode.ARROW

Arrow

documentView.drawMode = DocView.AnnotMode.LINE

When a “Drawing Annotation Mode” is disabled, the annotation is saved to the document.

Getting Draw Mode#

To get the current draw mode use the getDrawMode method:

val dm:DocView.AnnotMode = documentView.drawMode

Styling Drawing Annotations#

Drawing annotations can be styled as follows:

Opacity#

To get/set the opacity use the API as follows:

val color:Int = documentView.lineColor // getter
documentView.lineColor = 0xFF00FF00 // setter

Line Thickness#

To get/set the line thickness use the API as follows:

val thickness:Float = documentView.lineThickness // getter
documentView.lineThickness = 2.0f // setter

Line Color#

To get/set the line color use the API as follows:

val color:Int = documentView.lineColor // getter
documentView.lineColor = 0xFF00FF00 // setter

Line Endings#

Line endings are only supported for the DocView.AnnotMode.LINE annotation. They can be set against the start & end of the line as follows:

documentView.setLineEndStyles(DocumentViewPdf.LINE_ENDING_BUTT, DocumentViewPdf.LINE_ENDING_CIRCLE)

Line ending enumeration#

Style

Description

DocumentViewPdf.LINE_ENDING_NONE

None

DocumentViewPdf.LINE_ENDING_BUTT

Butt

DocumentViewPdf.LINE_ENDING_SLASH

Slash

DocumentViewPdf.LINE_ENDING_CIRCLE

Cirle

DocumentViewPdf.LINE_ENDING_DIAMOND

Diamond

DocumentViewPdf.LINE_ENDING_OPEN_ARROW

Open arrow

DocumentViewPdf.LINE_ENDING_R_OPEN_ARROW

Right pointing open arrow

DocumentViewPdf.LINE_ENDING_SQUARE

Square

DocumentViewPdf.LINE_ENDING_CLOSED_ARROW

Closed arrow

DocumentViewPdf.LINE_ENDING_R_CLOSED_ARROW

Right pointing closed arrow

Document Text Annotations#

Document text annotations depend on a “Text Annotation Mode” being activated. The following API allows for control over setting this mode as well as finding out what mode is currently active.

Find the Document Text Annotation Mode#

val isHighlightMode:Boolean = documentView.isTextHighlightModeOn
val isTextSquigglyModeOn:Boolean = documentView.isTextSquigglyModeOn
val isTextStrikeThroughModeOn:Boolean = documentView.isTextStrikeThroughModeOn
val isTextUnderlineModeOn:Boolean = documentView.isTextUnderlineModeOn

Toggle the text mode#

Text Highlight Mode#

documentView.toggleTextHighlightMode()

Text Squiggly Mode#

documentView.toggleTextSquigglyMode()

Text Strikethrough Mode#

documentView.toggleTextStrikeThroughMode()

Text Underline Mode#

documentView.toggleTextUnderlineMode()

Highlighting Selected Text#

Another quick method to highlight selected text is to call the highlightSelection() method against your DocumentView instance.

documentView.highlightSelection()

Note

Without a text selection being present in your document view calling highlightSelection() will have no effect.

Document Placement Annotations#

Document placement annotations depend on a “Placement Annotation Mode” being activated. The following API allows for control over setting this mode as well as finding out what mode is currently active.

Find the Document Placement Annotation Mode#

val isNoteModeOn:Boolean = documentView.isNoteModeOn
val isTextModeOn:Boolean = documentView.isTextModeOn
val isStampModeOn:Boolean = documentView.isStampModeOn
val isAttachmentModeOn:Boolean = documentView.isAttachmentModeOn
val isLinkModeOn:Boolean = documentView.isLinkModeOn

Setting Note Mode#

Setting Note Mode On#

documentView.setNoteModeOn()

Setting Note Mode Off#

documentView.setNoteModeOff()

Setting Text Mode#

Setting Text Mode On#

documentView.setTextModeOn()

Setting Text Mode Off#

documentView.setTextModeOff()

Setting Stamp Mode#

Setting Stamp Mode On#

documentView.setStampModeOn()

Setting Stamp Mode Off#

documentView.setStampModeOff()

Setting Attachment Mode#

Setting Attachment Mode On#

documentView.setAttachmentModeOn()

Setting Attachment Mode Off#

documentView.setAttachmentModeOff()

Deleting Annotations#

Essentially all annotations are treated the same as simply selections once they have been selected by a user. This selection can then be removed by calling the deleteSelection() method against the DocumentView instance.

import com.artifex.sonui.editor.DocumentView

fun deleteDocumentSelection(documentView:DocumentView)  {
    documentView.deleteSelection()
}

Note

Without an annotation being currently selected in your document view calling deleteSelection() will have no effect.

Author#

To get/set the annotation author use the API as follows:

val author:String = documentView.author // getter
documentView?.author = "John Doe" // setter

This software is provided AS-IS with no warranty, either express or implied. This software is distributed under license and may not be copied, modified or distributed except as expressly authorized under the terms of that license. Refer to licensing information at artifex.com or contact Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, CA 94129, USA, for further information.Discord logo