<分区>
我想突出显示 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();