Преглед на файлове

add: 文字选中新增获取当前选中文字的坐标和页数

liutian преди 5 месеца
родител
ревизия
3ba75ee7d9
променени са 2 файла, в които са добавени 32 реда и са изтрити 0 реда
  1. 16 0
      packages/core/src/index.js
  2. 16 0
      packages/core/src/pdf_page_view.js

+ 16 - 0
packages/core/src/index.js

@@ -722,6 +722,22 @@ class ComPDFKitViewer {
     }).join('\n')
   }
 
+  // 获取选中的文字的坐标数据
+  getSelectedTextQuads(pageNumber) {
+    if (pageNumber) {
+      const page = this.pdfViewer._pages[pageNumber - 1]
+      if (page) {
+        return page.selectedTextQuads
+      }
+    }
+
+    const textQuads = this.pdfViewer._pages.map(function(page) {
+      return page.selectedTextQuads
+    }).filter((selectedTextQuad) => selectedTextQuad !== null)
+
+    return textQuads.length ? textQuads : null
+  }
+
   convertAnnotation(rawAnnotations) {
     const annotations = []
 

+ 16 - 0
packages/core/src/pdf_page_view.js

@@ -363,6 +363,22 @@ class PDFPageView {
     return selection ? selection.textContent : null
   }
 
+  get selectedTextQuads() {
+    const textSelection = this.textSelection
+    if (!textSelection) return null
+    const selection = textSelection._selection
+    if (selection) {
+      const textQuads = textSelection.rects
+      const rect = textSelection.rect
+      return {
+        textQuads,
+        rect,
+        pageIndex: this.pageIndex
+      }
+    }
+    return null
+  }
+
   #setDimensions() {
     const { viewport } = this;
     if (this.pdfPage) {