iText7创建自由文本注释及添加至指定关键字的位置
整理一篇文档的原因是主要是因为项目中要求注释使用文本框的自由文本注释,然后再搜索引擎上找自由文本的注释资料有点少,只能自己边摸索边实现,幸运的是还是摸索出来方法,完成了需求。
以下为具体的实现内容。
版本及maven依赖
创建自由文本类型的注释
以在(100, 500)位置内容为“abc-你好”的注释为例
String text = "abc-你好";
//注释的文本对象
PdfString annoContent = new PdfString(text, PdfEncodings.UNICODE_BIG);
float x = 100f;
float y = 500f;
//宽度计算 参照上一篇
float width = getTextWidth(text);
float height = 20f;
//注释的展示区域 以x,y为起点创建以width为宽和height为高的矩形局域
Rectangle rect = new Rectangle(x, y, width, height);
//注释文本字体配置
float fontSize = 12f;
DeviceRgb fontColor = DeviceRgb.BLACK;
AnnotationDefaultApperance da = new AnnotationDefaultApperance();
da.setFontSize(fontSize);
da.setColor(fontColor);
//只能使用StandardAnnotationFont和ExtendedAnnotationFont下的字体
da.setFont(StandardAnnotationFont.HelveticaBold);
//创建注释对象
DeviceRgb bgColor = DeviceRgb.YELLOW;
PdfFreeTextAnnotation annotation = new PdfFreeTextAnnotation(rect, annoContent);
//背景颜色
annotation.setColor(bgColor);
//字体配置
annotation.setDefaultAppearance(da);
getTextWidth方法详情参照: 计算文本宽度
将注释添加至pdf - 绝对定位
例如在第一页加上上边的注释
String pdfPath = "test.pdf";
PdfWriter pw = new PdfWriter(pdfPath);
PdfDocument pdfDoc = new PdfDocument(pw);
PdfPage page = pdfDoc.getFirstPage();
page.addAnnotation(annotation);
pdfDoc.close();
pw.close();
进阶使用-给指定的关键字添加注释
例如为关键字“aaa”添加注释“你好”
实现思路为先将文档复制一份新的文档
然后通过正则匹配,搜索文档中的关键字位置,然后输出注释至新文档
这样做的原因是iText不支持未关闭的文档进行搜索,而且reader不支持写入
//源文件
String sourcePath = "test.pdf";
PdfReader pr = new PdfReader(sourcePath);
PdfDocument sourcePd = new PdfDocument(pr);
//生成的新文件
String targetPath = "test-new.pdf";
PdfWriter pw = new PdfWriter(targetPath);
PdfDocument targetPd = new PdfDocument(pw);
//现将源文件的内容复制到新文件
float totalPages = sourcePd.getNumberOfPages();
sourcePd.copyPagesTo(1, totalPages, targetPd);
//正则表达式
String searchText = "aaa";
//文档内容读取转换
PdfDocumentContentParser parser = new PdfDocumentContentParser();
//注释的文本对象
PdfString annoContent = new PdfString("你好", PdfEncodings.UNICODE_BIG);
//宽度计算 参照上一篇
float width = getTextWidth("你好");
float height = 20f;
//注释文本字体配置
float fontSize = 12f;
DeviceRgb fontColor = DeviceRgb.BLACK;
AnnotationDefaultApperance da = new AnnotationDefaultApperance();
da.setFontSize(fontSize);
da.setColor(fontColor);
//只能使用StandardAnnotationFont和ExtendedAnnotationFont下的字体
da.setFont(StandardAnnotationFont.HelveticaBold);
//背景色
DeviceRgb bgColor = DeviceRgb.YELLOW;
for(int i = 1; i <= totalPages; i++){
//创建匹配对象
RegexBasedLocationExtractionStrategy strategy = new RegexBasedLocationExtractionStrategy(searchText);
RegexBasedLocationExtractionStrategy targetStrategy = parser.processContent(i, strategy);
//获取匹配结果
Collection<IPdfTextLocation> resultantLocations = strategy.getResultantLocations();
for (IPdfTextLocation location : resultantLocations) {
Rectangle textRectangle = location.getRectangle();
float x = textRectangle.getX();
float y = textRectangle.getY();
float localWidth = textRectangle.getWidth();
//注释的展示区域 以x,y为起点创建以width为宽和height为高的矩形局域
Rectangle rect = new Rectangle(x+localWidth, y, width, height);
PdfFreeTextAnnotation annotation = new PdfFreeTextAnnotation(rect, annoContent);
//背景颜色
annotation.setColor(bgColor);
//字体配置
annotation.setDefaultAppearance(da);
targetPd.getPage(i).addAnnotation(annotation);
}
}
targetPd.close();
pw.close();
sourcePd.close();
pr.close();