无法读取跨行突出显示的确切文本

我正在使用PDBox阅读PDF文档中突出显示的内容。 我能够以单行和多个单词的forms阅读突出显示的文本。 但是,我无法阅读突出显示的文字。 请查看以下示例代码以阅读突出显示的文本。

 PDDocument pddDocument = PDDocument.load(new File("C:\\pdf-sample.pdf")); List allPages = pddDocument.getDocumentCatalog().getAllPages(); for (int i = 0; i < allPages.size(); i++) { int pageNum = i + 1; PDPage page = (PDPage) allPages.get(i); List la = page.getAnnotations(); if (la.size() < 1) { continue; } System.out.println("Page number : "+pageNum); for (PDAnnotation pdfAnnot: la) { if (pdfAnnot.getSubtype().equals("Popup")) { continue; } PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition(true); PDRectangle rect = pdfAnnot.getRectangle(); float x = rect.getLowerLeftX() - 1; float y = rect.getUpperRightY() - 1; float width = rect.getWidth(); float height = rect.getHeight() + rect.getHeight() / 4; int rotation = page.findRotation(); if (rotation == 0) { PDRectangle pageSize = page.getMediaBox(); y = pageSize.getHeight() - y; } Rectangle2D.Float awtRect = new Rectangle2D.Float(x, y, width, height); stripper.addRegion(Integer.toString(0), awtRect); stripper.extractRegions(page); System.out.println("------------------------------------------------------------------"); System.out.println("Annot type = " + pdfAnnot.getSubtype()); System.out.println("Getting text from region = " + stripper.getTextForRegion(Integer.toString(0)) + "\n"); System.out.println("Getting text from comment = " + pdfAnnot.getContents()); } } 

在读取突出显示的文本时,“pdfAnnot.getRectangle()”函数返回文本周围的最小矩形区域。 这提供了比所需更多的文本。 我找不到任何API来提取精确突出显示的文本。

例如:从测试PDF文件中提取的文本。

任何地方的任何人都可以打开PDF文件。 您只需要免费的Adobe Acrobat

读者 。 其他文件格式的收件人有时无法打开文件,因为它们

没有用于创建文档的应用程序。

用例1:阅读第一个粗体文本,即PDF 。 阅读单行中突出显示的文本没有问题。 将打印正确的文本,如下所示:
输出:从region =“ PDF ”获取文本

用例2:读取第二个粗体文本,即Adobe Acrobat阅读器 ,分为两行。 在这种情况下,运行上述程序的提取文本是:
输出:从区域获取文本=“ 任何人,任何地方都可以打开PDF文件。您只需要免费的Adobe Acrobat Reader。其他文件格式的收件人有时无法打开文件,因为它们 ”。

getRectangle()API给出了由突出显示的文本包围的最小矩形的坐标。 因此,它比“Adobe Acrobat Reader”更多文本。

  1. 如何知道提取区域中突出显示的起点和终点。
  2. 如何知道提取区域中的行数。

任何帮助将受到高度赞赏。

我设法使用以下代码提取突出显示的文本。

 // PDF32000-2008 // 12.5.2 Annotation Dictionaries // 12.5.6 Annotation Types // 12.5.6.10 Text Markup Annotations @SuppressWarnings({ "unchecked", "unused" }) public ArrayList getHighlightedText(String filePath, int pageNumber) throws IOException { ArrayList highlightedTexts = new ArrayList<>(); // this is the in-memory representation of the PDF document. // this will load a document from a file. PDDocument document = PDDocument.load(filePath); // this represents all pages in a PDF document. List allPages = document.getDocumentCatalog().getAllPages(); // this represents a single page in a PDF document. PDPage page = allPages.get(pageNumber); // get annotation dictionaries List annotations = page.getAnnotations(); for(int i=0; i 1) { str = str.concat(highlightedText); } else { str = highlightedText; } } highlightedTexts.add(str); } } document.close(); return highlightedTexts; }