【问题标题】:How to make PDF text selectable using Apache PDFBox?如何使用 Apache PDFBox 使 PDF 文本可选择?
【发布时间】:2016-01-16 21:34:20
【问题描述】:

我正在尝试在 JavaFX 上制作的 PDF 阅读应用程序中选择文本。我有包含带有文本和 OCR 层的屏幕截图的 PDF 文件。所以我需要像普通查看器一样选择文本。我设置了从页面获取图像,现在试图弄清楚如何突出显示文本。

我尝试了以下操作:

    InputStream is = this.getClass().getResourceAsStream(currentPdf);
    Image convertedImage;
    try {
        PDDocument document = PDDocument.load(is);
        List<PDPage> list = document.getDocumentCatalog().getAllPages();
        PDPage page = list.get(pageNum);
        List annotations = page.getAnnotations();
        PDAnnotationTextMarkup markup = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
        markup.setRectangle(new PDRectangle(600, 600));
        markup.setQuadPoints(new float[]{100, 100, 200, 100, 100, 500, 200, 500});
        annotations.add(markup);
        page.setAnnotations(annotations);
        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 128);
        convertedImage = SwingFXUtils.toFXImage(image, null);
        document.close();
        imageView.setImage(convertedImage);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

但这会导致图像没有任何亮点。

我也尝试在堆栈溢出或其他资源处查找信息,但没有找到任何信息。

希望有一些 Java 代码示例可以使用鼠标突出显示文本。

【问题讨论】:

  • 请上传PDF。
  • 这里是示例bit.ly/1OWKAa1
  • 好在它确实有文字。在 PDFBox 2.0 中,有工具 DrawPrintTextLocations.java,请尝试一下。您的问题不清楚,您想要一个具有文本标记功能的查看器,还是想要突出显示内容然后保存 PDF?
  • 基本上您将 PDF 绘制为位图图像(因此会丢失所有像素为文本和不是文本的信息)并显示该图像。因此,您需要告诉 javafx 文本在哪里。
  • @Polyakoff 还查看了 ExtractTextByArea.java 示例,这将为您获取选定区域的文本。

标签: java apache pdf javafx pdfbox


【解决方案1】:

我使用了 ICEpdf 并做了以下操作:

question.getSelectedBounds()
                .stream()
                .map(Shape::getBounds)
                .forEach(bounds -> {
                    SquareAnnotation squareAnnotation = (SquareAnnotation)
                            AnnotationFactory.buildAnnotation(
                                    pdfController.getPageTree().getLibrary(),
                                    Annotation.SUBTYPE_SQUARE,
                                    bounds);
                    squareAnnotation.setFillColor(true);
                    squareAnnotation.setFillColor(new Color(255, 250, 57, 120));
                    squareAnnotation.setRectangle(bounds);
                    squareAnnotation.setBBox(bounds);
                    squareAnnotation.resetAppearanceStream(null);
                    AbstractAnnotationComponent annotationComponent = AnnotationComponentFactory
                            .buildAnnotationComponent(squareAnnotation, pdfController.getDocumentViewController(),
                                    pageViewComponent, pdfController.getDocumentViewController().getDocumentViewModel());
                    pageViewComponent.addAnnotation(annotationComponent);
                });

【讨论】:

    猜你喜欢
    • 2014-07-11
    • 2021-01-05
    • 2013-03-22
    • 2017-01-29
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2015-03-23
    相关资源
    最近更新 更多