I'm trying to find certain text in a pdf and making the font color white. 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
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. This is what I have so far (don't mind the style, just a POC):
TextPositionSequence class by mkl
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();
}
}
}