Skip to content
Guides

Get the Selected Content

Users can select text in a PDF document by long pressing their finger and get the selected content.

This example shows how to Get the selected content:

  1. Extend the CPDFContextMenuShowHelper class and implement the getSelectTextContentView() method.
java
public class CPDFContextMenuHelper extends CPDFContextMenuShowHelper {

  public CPDFContextMenuHelper(CPDFReaderView readerView) {
    super(readerView);
  }

  /**
   * Long press text listener
   */
  @Override
  public View getSelectTextContentView(CPDFPageView cpdfPageView, LayoutInflater layoutInflater) {
    ContextMenuView menuView = new ContextMenuView(readerView.getContext());
    // Add a menu item to retrieve the selected text content on button click
    menuView.addItem(R.string.tools_get_content, v -> {
      // Get the text selection interface
      ISelectionHelper selectionHelper = cpdfPageView.getSelectionHelper();
      // Get all selected text areas on the current page
      List<CPDFTextSelection> selections = selectionHelper.getSelections();
      // Get the original page size of the specified page number, converting PDF page size to Android coordinates
      RectF size = readerView.getPageNoZoomSize(cpdfPageView.getPageNum());
      CPDFPage page = readerView.getPDFDocument().pageAtIndex(cpdfPageView.getPageNum());
      // CPDFTextPage handles text-related functions
      CPDFTextPage textPage = page.getTextPage();

      int length = selections.size();
      RectF annotRect = new RectF();
      RectF[] quadRects = new RectF[length];
      StringBuilder markedText = new StringBuilder();
      // The selected text may span multiple lines, iterate to retrieve the selected text
      for (int i = 0; i < length; i++) {
        CPDFTextSelection textSelection = selections.get(i);
        if (textSelection == null) continue;
        // Get the original position of the selected text in the PDF document
        RectF rect = new RectF(textSelection.getRectF());
        // Convert to screen coordinates
        rect = page.convertRectFromPage(readerView.isCropMode(), size.width(), size.height(), rect);
        if (annotRect.isEmpty()) {
          annotRect.set(rect);
        } else {
          annotRect.union(rect);
        }
        quadRects[i] = new RectF(textSelection.getRectF());
        // Retrieve the text within the rectangle
        String text = textPage.getText(textSelection.getTextRange());
        if (!TextUtils.isEmpty(text)) {
          markedText.append(text);
        }
      }
      System.out.println("markedText:" + markedText);
    });
    return menuView;
  }
}
kotlin
class CPDFContextMenuHelper(readerView: CPDFReaderView?) : CPDFContextMenuShowHelper(readerView) {
    /**
     * Long press text listener
     */
    override fun getSelectTextContentView(
        cpdfPageView: CPDFPageView,
        layoutInflater: LayoutInflater?
    ): View {
        val menuView: ContextMenuView = ContextMenuView(readerView.context)
        // Add a menu item to retrieve the selected text content on button click
        menuView.addItem(R.string.tools_get_content) { v ->
            // Get the text selection interface
            val selectionHelper = cpdfPageView.selectionHelper
            // Get all selected text areas on the current page
            val selections = selectionHelper.selections
            // Get the original page size of the specified page number, converting PDF page size to Android coordinates
            val size = readerView.getPageNoZoomSize(cpdfPageView.pageNum)
            val page = readerView.pdfDocument.pageAtIndex(cpdfPageView.pageNum)
            // CPDFTextPage handles text-related functions
            val textPage = page.textPage

            val length = selections.size
            val annotRect = RectF()
            val quadRects = arrayOfNulls<RectF>(length)
            val markedText = StringBuilder()
            // The selected text may span multiple lines, iterate to retrieve the selected text
            for (i in 0 until length) {
                val textSelection = selections[i] ?: continue
                // Get the original position of the selected text in the PDF document
                var rect: RectF? = RectF(textSelection.rectF)
                // Convert to screen coordinates
                rect = page.convertRectFromPage(
                    readerView.isCropMode,
                    size.width(),
                    size.height(),
                    rect
                )
                if (annotRect.isEmpty) {
                    annotRect.set(rect)
                } else {
                    annotRect.union(rect)
                }
                quadRects[i] = RectF(textSelection.rectF)
                // Retrieve the text within the rectangle
                val text = textPage.getText(textSelection.textRange)
                if (!TextUtils.isEmpty(text)) {
                    markedText.append(text)
                }
            }
            println("markedText:$markedText")
        }
        return menuView
    }
}
  1. Set the custom context menu in CPDFReaderView.
java
// Set the custom context menu
cpdfReaderView.setContextMenuShowListener(new CPDFContextMenuHelper(readerView));

Following these steps, you can retrieve and manipulate selected text content through the context menu when long-pressing on the text.