Custom Menu
When viewing a PDF, if a user selects text or long-presses on a blank area, the PDF enters the corresponding interaction state. At this point, the system will display a context menu to perform operations related to the current interaction.
By inheriting CPDFContextMenuShowHelper and overriding the appropriate methods, you can customize the menu layout, content, and interaction behavior. For example, the context menu for a highlight annotation can include actions such as “Modify Properties,” “Copy Text,” and “Delete Annotation.”
Operation Flow Diagram
User Action
│
│ Select text / Long-press blank area
▼
PDF enters corresponding interaction state
│
│ Context menu appears
▼
Custom menu item clicked
│
│ Execute corresponding operation (modify properties / copy text / delete annotation, etc.)
▼
Refresh PDF page / update UI2
3
4
5
6
7
8
9
10
11
12
13
Example Diagram (Highlight Annotation):
+---------------------+
| Highlight |
| Lorem ipsum... |
+---------------------+
│
▼ Long-press / Click menu
+---------------------+
| Properties | Copy | Delete | <- Custom Context Menu
+---------------------+2
3
4
5
6
7
8
9
Example: Custom Menu for Highlight Annotations
t_markup_annot_menu_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_context_menu_window"
android:orientation="horizontal">
<TextView
android:id="@+id/attr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Properties" />
<TextView
android:id="@+id/copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Copy" />
<TextView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
</LinearLayout>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class TContextMenuHelper extends CPDFContextMenuShowHelper {
@Override
public View getMarkupContentView(CPDFPageView pageView, CPDFBaseAnnotImpl annotImpl, LayoutInflater layoutInflater) {
View contentView = layoutInflater.inflate(R.layout.t_markup_annot_menu_layout, null);
CPDFMarkupAnnotImpl markupAnnotImpl = (CPDFMarkupAnnotImpl) annotImpl;
CPDFMarkupAnnotation markupAnnotation = markupAnnotImpl.onGetAnnotation();
// Set menu click actions
invokeOnClickListener(contentView, v -> {
int id = v.getId();
if (id == R.id.attr) { // Modify properties
markupAnnotation.setColor(Color.RED);
markupAnnotation.setAlpha(100);
markupAnnotation.updateAp();
markupAnnotImpl.onAnnotAttrChange();
pageView.invalidate();
} else if (id == R.id.copy) { // Copy text
String markedText = markupAnnotation.getMarkedText();
if (!TextUtils.isEmpty(markedText)) {
CPDFTextUtils.setClipData(context, "ComPDF", markedText);
}
} else if (id == R.id.delete) { // Delete annotation
pageView.deleteAnnotation(annotImpl);
}
popupWindow.dismiss();
}, R.id.attr, R.id.copy, R.id.delete);
return contentView;
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class TContextMenuHelper(readerView: CPDFReaderView?) : CPDFContextMenuShowHelper(readerView) {
override fun getMarkupContentView(
pageView: CPDFPageView,
annotImpl: CPDFBaseAnnotImpl<*>,
layoutInflater: LayoutInflater
): View {
val contentView = layoutInflater.inflate(R.layout.t_markup_annot_menu_layout, null)
val markupAnnotImpl = annotImpl as CPDFMarkupAnnotImpl
val markupAnnotation = markupAnnotImpl.onGetAnnotation()
// Set menu click actions
invokeOnClickListener(contentView, { v ->
when (v.id) {
R.id.attr -> { // Modify properties
markupAnnotation.color = Color.RED
markupAnnotation.alpha = 100
markupAnnotation.updateAp()
markupAnnotImpl.onAnnotAttrChange()
pageView.invalidate()
}
R.id.copy -> { // Copy text
val markedText = markupAnnotation.markedText
if (markedText.isNotEmpty()) {
CPDFTextUtils.setClipData(context, "ComPDF", markedText)
}
}
R.id.delete -> { // Delete annotation
pageView.deleteAnnotation(annotImpl)
}
}
popupWindow.dismiss()
}, R.id.attr, R.id.copy, R.id.delete)
return contentView
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Set CPDFReaderView to use the custom context menu:
getCPdfReaderView().setContextMenuShowListener(new TContextMenuHelper(readerView));List of Methods for Custom Context Menus
| Method | Applicable Annotation/Operation | Description |
|---|---|---|
| getShapeContentView | Shape annotations | Custom context menu for shapes |
| getFreetextContentView | FreeText annotations | Custom context menu for FreeText |
| getMarkupContentView | Highlight / Underline / Squiggly | Custom menu for markup annotations |
| getInkContentView | Ink / Signature annotations | Custom menu for ink annotations |
| getStampContentView | Stamp annotations | Custom menu for stamp annotations |
| getLinkContentView | Link annotations | Custom menu for link annotations |
| getNoteContentView | Note / Comment | Custom menu for note annotations |
| getSoundContentView | Audio annotations | Custom menu for audio annotations |
| getCheckboxContentView | Form - Checkbox | Custom menu for checkbox forms |
| getComboboxContentView | Form - ComboBox | Custom menu for dropdown forms |
| getListboxContentView | Form - ListBox | Custom menu for list box forms |
| getPushbuttonContentView | Form - Button | Custom menu for button forms |
| getRadiobuttonContentView | Form - RadioButton | Custom menu for radio buttons |
| getSignatureContentView | Digital Signature | Custom menu for signature fields |
| getTextfieldContentView | Form - TextField | Custom menu for text fields |
| getSelectTextContentView | Text selection | Custom menu for selected text |
| getEditImageAreaContentView | Image editing area | Custom menu for image editing |
| getLongPressContentView | Long-press on blank area | Menu for blank area long-press |
| getEditLongPressContentView | Long-press in edit mode | Menu for edit mode long-press |
| getEditTextAreaContentView | Text area editing | Menu for text editing area |
| getEditSelectTextContentView | Edit mode selected text | Menu for selected text in edit mode |
| getEditTextContentView | Edit text | Menu for editing text |
| getRedactContentView | Redact / Hide content | Menu for redaction actions |
| getCropImageAreaContentView | Image cropping | Menu for image cropping |
| getSearchReplaceContentView | Search / Replace | Menu for search & replace |
| getEditPathAreaContentView | Path editing | Menu for path editing |
| getScreenShotContentView | Screenshot area | Menu for screenshot actions |
Note: All methods in the table can be overridden by inheriting
CPDFContextMenuShowHelperto implement custom menus. Each method corresponds to a specific annotation type or operation type, allowing developers to customize menu layout and behavior as needed.