简体   繁体   English

PdfBox - 更改矩形中的字体颜色

[英]PdfBox - changing font color in rectangle

I'm trying to find certain text in a pdf and making the font color white.我正在尝试在 pdf 中查找某些文本并将字体颜色设为白色。 As a POC I've already succeeded finding text and highlighting it in the pdf based on the code written by mkl here: find position of text in pdf作为 POC,我已经根据 mkl 在这里编写的代码成功找到文本并在 pdf 中突出显示它: find position of text in pdf

Is it however possible, based on the received coordinates to change the font color of the text inside the rectangle instead of highlighting the text?然而,是否有可能根据接收到的坐标更改矩形内文本的字体颜色而不是突出显示文本? Alternatively, can I add a white rectangle to cover the text?或者,我可以添加一个白色矩形来覆盖文本吗?

Thanks in advance提前致谢

edit: I have started adding the rectangles to the pdf, however as stated they are not in correct position.编辑:我已经开始将矩形添加到 pdf 中,但是正如所述,它们的位置不正确。 This is what I have so far (don't mind the style, just a POC):这是我到目前为止所拥有的(不要介意风格,只是一个 POC):

TextPositionSequence class by mkl 由 mkl 提供的 TextPositionSequence 类

byte[] content = ...;
PDDocument document = PDDocument.load(content);
for (int page = 1; page <= document.getNumberOfPages(); page++) {
            List<TextPositionSequence> hits = null;
            try {
                hits = findSubwordsImproved(document, page, "[" + searchTerm + "]");
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (TextPositionSequence hit : hits) {
                TextPosition lastPosition = hit.textPositionAt(hit.length() - 1);
                TextPosition firstPosition = hit.textPositionAt(0);

                PDPage actualPage = document.getPage(page - 1);

                PDRectangle cropBox = actualPage.getCropBox();

                float x = firstPosition.getTextMatrix().getTranslateX() + cropBox.getLowerLeftX();
                float y = firstPosition.getTextMatrix().getTranslateY() + cropBox.getLowerLeftY();
                float w = hit.getWidth();
                try {
                    PDPageContentStream contents = new PDPageContentStream(document, actualPage, PDPageContentStream.AppendMode.APPEND, false);
                    contents.setNonStrokingColor(Color.RED);
                    contents.addRect(x, y, w, firstPosition.getHeight());
                    contents.fill();
                    contents.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
           }
}

I've solved it with the following code.我已经用下面的代码解决了它。 I fiddled with the rectangle height a bit to get the box to cover the entire text.我稍微调整了矩形的高度,以使框覆盖整个文本。 This might need tweaking in the future:这可能需要在未来进行调整:

float posXInit = hit.getX();
float posXEnd = lastPosition.getXDirAdj() + lastPosition.getWidth();
float posYInit = firstPosition.getPageHeight() - firstPosition.getYDirAdj();
float posYEnd = firstPosition.getPageHeight() - lastPosition.getYDirAdj();
float height = firstPosition.getHeight();

PDPageContentStream contents = new PDPageContentStream(document, actualPage, PDPageContentStream.AppendMode.APPEND, false, true);
contents.setNonStrokingColor(Color.WHITE);
contents.addRect(posXInit, posYEnd - height / 3, hit.getWidth(), height * 2);
contents.fill();
contents.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM