Existing requirements: search for the specified text in the pdf file, replace the specified text, and record the page, row and column where the modified text is located. I am using the spire jar package, which has implemented search and replacement. The number of pages can be recorded with api, but I don't know how to record the rows and columns. There are many on the website that use the coordinate method to locate.
public static void main(String[] args) {
//加载示例PDF文档
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("D:\\Spire\\Input\\test05.pdf");
//遍历文档
for (int i = 0; i < pdf.getPages().getCount(); i++)
{
//获取所有页面
PdfPageBase page = pdf.getPages().get(i);
//查找指定文本
PdfTextFindCollection textFindCollection;
textFindCollection = page.findText("内容提要");
//创建画刷、字体
PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.red));
PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("宋体",Font.PLAIN,12),true);
//用新的文本替换原有文本
Rectangle2D rec;
for(PdfTextFind find: textFindCollection.getFinds())
{
rec = find.getBounds();
System.out.println(rec);
page.getCanvas().drawRectangle(PdfBrushes.getWhite(), rec);
page.getCanvas().drawString("Coffee", font, brush, rec);
}
}
//保存文档
pdf.saveToFile("D:\\Spire\\OutPut\\Replace.pdf");
pdf.close();
}