Highlight text PDF Box Reader… here is a solution to the problem.
Highlight text PDF Box Reader
I want to highlight specific text in a PDF. I wrote the following code, but I get an empty pdf with a box. I want to display the existing pdf content and should draw a box over the text so that it acts as a text highlight.
File file = new File(pdfName);
PDDocument document = PDDocument.load(file);
PDPage page = document.getPage(0);
Instantiating the PDPageContentStream class
PDPageContentStream contentStream = new PDPageContentStream(document, page);
Setting the non stroking color
contentStream.setNonStrokingColor(Color.DARK_GRAY);
Drawing a rectangle
contentStream.addRect(data.get(0).getX(), data.get(0).getY(), data.get(0).getWidth(), data.get(0).getHeight());
Drawing a rectangle
contentStream.fill();
System.out.println("rectangle added");
Closing the ContentStream object
contentStream.close();
Saving the document
File file2 = new File("CompareOutput.pdf");
File fileOutput = new File("CompareOutput.pdf");
document.save("CompareOutput.pdf");
Closing the document
document.close();