iText7单字符Text元素自定义TextRenderer跨行下划线问题咨询
自定义下划线跨行走线断裂问题解决方案
原实现缺陷分析
- 仅对首尾标记字符传状态,未标记下划线区间内的普通字符,无法绘制区间中间的连续线段
- 未处理跨行、跨页场景下的坐标衔接逻辑,导致换行后划线直接中断
优化实现方案
第一步:调整文本拆分逻辑
增加区间状态标记,将「是否处于下划线区间」的状态同步传给TextRenderer,代码如下:
String s = "d < like to filter > RENDER_TEXT events as they are <written> to an output file. I have a <PDF that has some text in it> that I want filtered out. I've found that I can walk the document once and determine the characteristics of the render events that I want to filter. Now I'd like to copy the pages of the source document and skip over some RENDER_TEXT events so that the text does not appear in the destination document. I have an IEventFilter that will accept the correct events. I just need to know how to put this filter on the document writer."; String[] words = s.split(""); Paragraph paragraph = new Paragraph(); Text text = null; boolean isUnderStart = false; boolean isUnderEnd = false; boolean isInUnderline = false; // 新增:标记当前字符是否处于下划线区间内 int i = 1; for (String word : words) { if (word.equals("<")) { isUnderStart = true; isInUnderline = true; // 若不需要显示<标记可保留continue,需要显示则删除该行即可 continue; } if (word.equals(">")) { isUnderEnd = true; isInUnderline = false; // 若不需要显示>标记可保留continue,需要显示则删除该行即可 continue; } text = new Text(word); // 新增传入isInUnderline状态 text.setNextRenderer(new Word25TextRenderer(text, isUnderStart, isUnderEnd, isInUnderline)); isUnderStart = false; isUnderEnd = false; paragraph.add(text); i++; } doc.add(paragraph); doc.close();
第二步:重写TextRenderer逻辑,增加跨行衔接处理
使用ThreadLocal缓存上一个划线字符的坐标、页码、行高信息,处理跨行/跨页时的划线衔接,代码如下:
static class Word25TextRenderer extends TextRenderer { private boolean isUnderStart; private boolean isUnderEnd; private boolean isInUnderline; // 缓存上一个划线节点的坐标、页码信息,ThreadLocal避免多线程渲染冲突 private static ThreadLocal<float[]> lastPoint = ThreadLocal.withInitial(() -> new float[]{-1, -1, -1}); // 存储格式:x坐标,y坐标,页码 private static ThreadLocal<Float> lineHeight = ThreadLocal.withInitial(() -> 0f); public Word25TextRenderer(Text textElement, boolean isUnderStart, boolean isUnderEnd, boolean isInUnderline) { super(textElement); this.isUnderStart = isUnderStart; this.isUnderEnd = isUnderEnd; this.isInUnderline = isInUnderline; } @Override public IRenderer getNextRenderer() { return new Word25TextRenderer((Text) modelElement, isUnderStart, isUnderEnd, isInUnderline); } @Override public void draw(DrawContext drawContext) { super.draw(drawContext); // 不在下划线区间也不是结束标记,直接跳过 if (!isInUnderline && !isUnderEnd) { return; } Rectangle textRect = getOccupiedAreaBBox(); int currentPage = getOccupiedArea().getPageNumber(); PdfCanvas canvas = drawContext.getCanvas(); float underY = textRect.getBottom() - 2; // 可自定义调整下划线与文字的间距 lineHeight.set(textRect.getHeight()); // 处理下划线起点 if (isUnderStart) { canvas.saveState() .setStrokeColor(DeviceRgb.RED) .setLineWidth(1f); // 可自定义线宽 lastPoint.set(new float[]{textRect.getLeft(), underY, currentPage}); return; } float[] last = lastPoint.get(); // 同一页同一行的情况,直接连直线 if (last[2] == currentPage && Math.abs(last[1] - underY) < 2) { canvas.moveTo(last[0], last[1]) .lineTo(textRect.getRight(), underY) .stroke(); lastPoint.set(new float[]{textRect.getRight(), underY, currentPage}); } // 跨行/跨页的情况,上一行画到页面右边界,新行重新开始绘制 else if (last[2] != -1) { // 这里的36f是iText默认的页面右边距,可替换为你实际设置的段落右边距 float pageRight = drawContext.getDocument().getPage(currentPage).getPageSize().getRight() - 36f; canvas.moveTo(last[0], last[1]) .lineTo(pageRight, last[1]) .stroke(); // 新行从当前字符左侧开始画 canvas.moveTo(textRect.getLeft(), underY) .lineTo(textRect.getRight(), underY) .stroke(); lastPoint.set(new float[]{textRect.getRight(), underY, currentPage}); } // 处理下划线终点,绘制尾部曲线 if (isUnderEnd) { canvas.curveTo(textRect.getRight(), underY + lineHeight.get() / 2, textRect.getRight(), underY, textRect.getRight(), underY) .stroke() .restoreState(); // 重置缓存状态,避免影响下一段下划线 lastPoint.set(new float[]{-1, -1, -1}); lineHeight.remove(); } } }
优化说明
- 支持跨多行、多页的下划线连贯绘制,不会出现断裂问题
- 保留了单字符粒度的属性控制能力,可单独调整每个字符的字体、字号、颜色等
- 下划线间距、线宽、颜色、尾部曲线参数均可自定义调整,匹配需求效果
- ThreadLocal缓存避免多线程渲染场景下的状态冲突
内容的提问来源于stack exchange,提问作者cixikoy607
相关产品推荐
相关产品推荐

