有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java中使用PDFBox突出显示特定的单词或句子

有没有办法使用PDFBox突出显示特定的单词或句子

我找到了一种方法,可以使用下面的代码突出显示整个PDF页面或完整的行,但无法标记或突出显示特定的单个单词

在java中使用PDFBox可能吗?如果没有,那么有没有其他方法可以更好地实现这一点

public static void main(String[] args)  throws IOException {
    PDDocument document = null;
    String fileName = "/Desktop/under_test/PDF3.pdf";
    try {
        document = PDDocument.load( new File(fileName) );
        PDFTextStripper stripper = new Test2();
        stripper.setSortByPosition( true );

        stripper.setStartPage( 0 );
        stripper.setEndPage( document.getNumberOfPages() );

        Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
        stripper.writeText(document, dummy);

        File file1 = new File("/Desktop/under_test/PDF3_Highlighted.pdf");
        document.save(file1);
    }
    finally {
        if( document != null ) {
            document.close();
        }
    }
}

@Override
protected void writeString(String string, List<TextPosition> textPositions) throws IOException {
    boolean isFound = false;
    float posXInit  = 0, 
          posXEnd   = 0, 
          posYInit  = 0,
          posYEnd   = 0,
          width     = 0, 
          height    = 0, 
          fontHeight = 0;
    String[] criteria = {"pixel"};

    for (int i = 0; i < criteria.length; i++) {
        if (string.contains(criteria[i])) {
            isFound = true;
        } 
    }
    if (isFound) {
        posXInit = textPositions.get(0).getXDirAdj();
        posXEnd  = textPositions.get(textPositions.size() - 1).getXDirAdj() + textPositions.get(textPositions.size() - 1).getWidth();
        posYInit = textPositions.get(0).getPageHeight() - textPositions.get(0).getYDirAdj();
        posYEnd  = textPositions.get(0).getPageHeight() - textPositions.get(textPositions.size() - 1).getYDirAdj();
        width    = textPositions.get(0).getWidthDirAdj();
        height   = textPositions.get(0).getHeightDir();

        System.out.println(string + "X-Init = " + posXInit + "; Y-Init = " + posYInit + "; X-End = " + posXEnd + "; Y-End = " + posYEnd + "; Font-Height = " + fontHeight);

        /* numeration is index-based. Starts from 0 */

        float quadPoints[] = {posXInit, posYEnd + height + 2, posXEnd, posYEnd + height + 2, posXInit, posYInit - 2, posXEnd, posYEnd - 2};

        List<PDAnnotation> annotations = document.getPage(this.getCurrentPageNo() - 1).getAnnotations();
        PDAnnotationTextMarkup highlight = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);

        PDRectangle position = new PDRectangle();
        position.setLowerLeftX(posXInit);
        position.setLowerLeftY(posYEnd);
        position.setUpperRightX(posXEnd);
        position.setUpperRightY(posYEnd + height);

        highlight.setRectangle(position);

        // quadPoints is array of x,y coordinates in Z-like order (top-left, top-right, bottom-left,bottom-right) 
        // of the area to be highlighted

        highlight.setQuadPoints(quadPoints);

        PDColor yellow = new PDColor(new float[]{1, 1, 1 / 255F}, PDDeviceRGB.INSTANCE);
        highlight.setColor(yellow);
        annotations.add(highlight);
    }
}

共 (0) 个答案