Skip to content

获取选中内容

用户可以通过长按手指在PDF文档中选择文本,并获取选中的内容。

以下示例展示了如何获取选中的内容:

  1. 继承 CPDFContextMenuShowHelper 类,并实现 getSelectTextContentView() 方法。
java
public class CPDFContextMenuHelper extends CPDFContextMenuShowHelper {

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

  /**
   * 长按文本监听
   */
  @Override
  public View getSelectTextContentView(CPDFPageView cpdfPageView, LayoutInflater layoutInflater) {
    ContextMenuView menuView = new ContextMenuView(readerView.getContext());
    // 添加一个菜单项,用于点击按钮时获取选中的文本内容
    menuView.addItem(R.string.tools_get_content, v -> {
      // 获取文本选择接口
      ISelectionHelper selectionHelper = cpdfPageView.getSelectionHelper();
      // 获取当前页面中所有选中的文本区域
      List<CPDFTextSelection> selections = selectionHelper.getSelections();
      // 获取指定页码的原始页面大小,将PDF页面大小转换为Android坐标系
      RectF size = readerView.getPageNoZoomSize(cpdfPageView.getPageNum());
      CPDFPage page = readerView.getPDFDocument().pageAtIndex(cpdfPageView.getPageNum());
      // CPDFTextPage处理与文本相关的功能
      CPDFTextPage textPage = page.getTextPage();

      int length = selections.size();
      RectF annotRect = new RectF();
      RectF[] quadRects = new RectF[length];
      StringBuilder markedText = new StringBuilder();
      // 选中的文本可能跨越多行,遍历获取选中的文本
      for (int i = 0; i < length; i++) {
        CPDFTextSelection textSelection = selections.get(i);
        if (textSelection == null) continue;
        // 获取选中文本在PDF文档中的原始位置
        RectF rect = new RectF(textSelection.getRectF());
        // 转换为屏幕坐标
        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());
        // 获取矩形中的文本
        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. CPDFReaderView 中设置自定义的上下文菜单
java
// 设置自定义的上下文菜单
cpdfReaderView.setContextMenuShowListener(new CPDFContextMenuHelper(readerView));

通过以上步骤,您可以在长按选中文本后,通过上下文菜单获取并操作选中的文本内容。