iText7创建自由文本注释及添加至指定关键字的位置

本文介绍如何使用iText7在PDF文档中创建自由文本注释,并详细讲解了注释的绝对定位方法以及如何针对特定关键字添加注释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


整理一篇文档的原因是主要是因为项目中要求注释使用文本框的自由文本注释,然后再搜索引擎上找自由文本的注释资料有点少,只能自己边摸索边实现,幸运的是还是摸索出来方法,完成了需求。
以下为具体的实现内容。

版本及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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值