我想在PDF文档中突出显示特定的文本。我已经编写了以下代码,但是生成的PDF文档是空的,并且只有一个方框。我想要展示现有的PDF内容,并在文本上绘制一个方框,以便它可以作为文本高亮显示。
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();