Create, Move, and Delete Text and Images
ComPDFKit provides comprehensive methods for creating, moving, and deleting text and images.
Performing Actions Through CPDFReaderView
CPDFReaderView
provides basic interactive capabilities by default, allowing the user to create and delete text and images, drag and drop to move the position of images and text blocks, resize images and text blocks, etc. by using the mouse and keyboard.
Configure the Context Menu.
If you need to copy, paste, cut, or delete text or images, you can add these methods to the context menu through the setContextMenuShowListener
event of CPDFReaderView
.
This example shows how to configure the context menu:
edit_text_area_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:orientation="horizontal">
<TextView
android:id="@+id/edit_copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="center"
android:text="copy" />
<TextView
android:id="@+id/edit_paste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="center"
android:text="paste" />
<TextView
android:id="@+id/edit_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="center"
android:text="delete" />
</LinearLayout>
public class DemoContextMenuHelper extends CPDFContextMenuShowHelper {
public DemoContextMenuHelper(CPDFReaderView cpdfReaderView) {
super(cpdfReaderView);
}
@Override
public View getEditTextAreaContentView(CPDFPageView cpdfPageView, LayoutInflater layoutInflater, CPDFEditSelections cpdfEditSelections) {
View contentView = layoutInflater.inflate(R.layout.edit_text_area_menu_layout, null);
invokeOnClickListener(contentView, v -> {
try {
int id = v.getId();
if (id == R.id.edit_copy) {
pageView.operateEditTextArea(CPDFPageView.EditTextAreaFuncType.COPY);
} else if (id == R.id.edit_paste) {
pageView.operateEditText(CPDFPageView.EditTextFuncType.PASTE);
} else if (id == R.id.edit_delete) {
pageView.operateEditTextArea(CPDFPageView.EditTextAreaFuncType.DELETE);
}
} finally {
dismissContextMenu();
}
}, R.id.edit_copy, R.id.edit_paste, R.id.edit_delete);
return contentView;
}
}
...
// Initialize CPDFReaderView.
CPDFReaderView readerView = findViewById(R.id.readerView);
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
readerView.setPDFDocument(document);
// Set a custom context menu.
readerView.setContextMenuShowListener(new DemoContextMenuHelper(readerView));
class DemoContextMenuHelper(cpdfReaderView: CPDFReaderView) :
CPDFContextMenuShowHelper(cpdfReaderView) {
override fun getEditTextAreaContentView(
cpdfPageView: CPDFPageView,
layoutInflater: LayoutInflater,
cpdfEditSelections: CPDFEditSelections
): View {
val contentView: View = layoutInflater.inflate(R.layout.edit_text_area_menu_layout, null)
invokeOnClickListener(contentView, { v: View ->
try {
val id = v.id
if (id == R.id.edit_copy) {
pageView.operateEditTextArea(CPDFPageView.EditTextAreaFuncType.COPY)
} else if (id == R.id.edit_paste) {
pageView.operateEditText(CPDFPageView.EditTextFuncType.PASTE)
} else if (id == R.id.edit_delete) {
pageView.operateEditTextArea(CPDFPageView.EditTextAreaFuncType.DELETE)
}
} finally {
dismissContextMenu()
}
}, R.id.edit_copy, R.id.edit_paste, R.id.edit_delete)
return contentView
}
}
...
// Initialize CPDFReaderView
val readerView = findViewById<CPDFReaderView>(R.id.readerView)
val document = CPDFDocument(context)
document.open(pdfPath)
readerView.pdfDocument = document
// Set a custom context menu.
readerView.contextMenuShowListener = DemoContextMenuHelper(readerView)
Inserting Text and Images
You can specify the ability to insert text and image blocks through the beginEdit
method of CPDFEditManager
. The following code shows how to achieve this:
CPDFEditManager editManager = cpdfReaderView.getEditManager();
// Insert Image
editManager.beginEdit(CPDFEditPage.LoadImage);
// Insert Text
editManager.beginEdit(CPDFEditPage.LoadText);
// Cancel
editManager.beginEdit(CPDFEditPage.LoadNone);