突出显示文本 PDF 框 Reader
Highlight a text PDF Box Reader
我想突出显示 pdf.I 中的特定文本已经编写了以下代码但是我得到的是空 pdf box.I 想显示现有的 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();
而不是
PDPageContentStream contentStream = new PDPageContentStream(document, page);
使用
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, true))
这样您的新内容流就会被附加。
不过我预计会有另一个问题,您可能希望 "highlight" 是透明的。看看 this answer.
我想突出显示 pdf.I 中的特定文本已经编写了以下代码但是我得到的是空 pdf box.I 想显示现有的 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();
而不是
PDPageContentStream contentStream = new PDPageContentStream(document, page);
使用
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, true))
这样您的新内容流就会被附加。
不过我预计会有另一个问题,您可能希望 "highlight" 是透明的。看看 this answer.