gpt4 book ai didi

java - itext在工作中如何找出坐标文本?

转载 作者:行者123 更新时间:2023-12-02 01:31:41 26 4
gpt4 key购买 nike

我想知道添加到页面的文本的坐标。并在该文本上应用一些 Canvas 。 (例如自定义下划线或删除线或三角形内的文本)

我只需要某些词

List<String> listString = new ArraList();

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));

Document doc = new Document(pdfDoc, PageSize.A4);
for(String s:listString)
//**underlineWord** hidden it will be removed
if( s.contains("**underlineWord**")){
s.replace("**underlineWord**","");
Text text = new Text(s)
Float[] coords = getCoords(text)
Canvas canvas = new Canvas(...);
setCustomUderline(coords)
}


doc.add(new Paragraph(text) );

我知道 PdfCanvasProcessor,但我不使用它,因为我不知道某些单词(其中包含 **underlineWord**)

最佳答案

在 iText7 中,您可以使用自定义渲染器来实现此类任务。该技术如 DashedUnderline example 所示。 :

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
Document doc = new Document(pdfDoc);
doc.add(new Paragraph("This text is not underlined"));
Text text1 = new Text("This text is underlined with a solid line");
text1.setUnderline(1, -3);
doc.add(new Paragraph(text1));
Text text2 = new Text("This text is underlined with a dashed line");
text2.setNextRenderer(new DashedLineTextRenderer(text2));
doc.add(new Paragraph(text2));
doc.close();

即您只需将自定义的Renderer 设置为相关的Text 位即可。在手头的示例中,自定义 Renderer 类是

protected class DashedLineTextRenderer extends TextRenderer {
public DashedLineTextRenderer(Text textElement) {
super(textElement);
}

@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
Rectangle rect = this.getOccupiedAreaBBox();
PdfCanvas canvas = drawContext.getCanvas();
canvas
.saveState()
.setLineDash(3, 3)
.moveTo(rect.getLeft(), rect.getBottom() - 3)
.lineTo(rect.getRight(), rect.getBottom() - 3)
.stroke()
.restoreState();
}
}

如您所见,您可以覆盖 draw 以首先调用 super 实现来正常绘制文本。之后,您可以通过调用 getOc​​cupiedAreaBBox 来检索用于绘制文本的区域并将其用于您的任务,无论是用于装饰文本还是仅将位置存储在某处。

<小时/>

如果您想知道为什么这个示例位于 events 子包中...该示例对应于 iText5 示例,在 iText5 中您可以通过以下方式实现这样的任务将通用标记设置为相关的 Chunk 并在页面事件监听器的 onGenericTag 方法中监听该通用标记的方法,请参阅 this answer举个例子。

<小时/>

在您提出的评论中

Can i do it with table? (draw border with help canvas)

是的,您再次使用关联的渲染器,例如请参阅DottedLineCell example :

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
doc.add(new Paragraph("Table event"));
Table table = new Table(UnitValue.createPercentArray(3)).useAllAvailableWidth();
table.setNextRenderer(new DottedLineTableRenderer(table, new Table.RowRange(0, 2)));
table.addCell(new Cell().add(new Paragraph("A1")).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(new Paragraph("A2")).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(new Paragraph("A3")).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(new Paragraph("B1")).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(new Paragraph("B2")).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(new Paragraph("B3")).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(new Paragraph("C1")).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(new Paragraph("C2")).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(new Paragraph("C3")).setBorder(Border.NO_BORDER));
doc.add(table);
doc.add(new Paragraph("Cell event"));
table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
Cell cell = new Cell().add(new Paragraph("Test"));
cell.setNextRenderer(new DottedLineCellRenderer(cell));
cell.setBorder(Border.NO_BORDER);
table.addCell(cell.setBorder(Border.NO_BORDER));
doc.add(table);

doc.close();

此处第一个表显示了如何在关闭标准表格单元格边框后使用表渲染器执行此操作,而第二个表显示如何使用单元格渲染器执行此操作。

自定义的渲染器类是

private class DottedLineTableRenderer extends TableRenderer {
public DottedLineTableRenderer(Table modelElement, Table.RowRange rowRange) {
super(modelElement, rowRange);
}

@Override
public void drawChildren(DrawContext drawContext) {
super.drawChildren(drawContext);
PdfCanvas canvas = drawContext.getCanvas();
canvas.setLineDash(3f, 3f);
// first horizontal line
CellRenderer[] cellRenderers = rows.get(0);
canvas.moveTo(cellRenderers[0].getOccupiedArea().getBBox().getLeft(),
cellRenderers[0].getOccupiedArea().getBBox().getTop());
canvas.lineTo(cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getRight(),
cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getTop());

for (int i = 0; i < rows.size(); i++) {
cellRenderers = rows.get(i);
// horizontal lines
canvas.moveTo(cellRenderers[0].getOccupiedArea().getBBox().getX(),
cellRenderers[0].getOccupiedArea().getBBox().getY());
canvas.lineTo(cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getRight(),
cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getBottom());
// first vertical line
Rectangle cellRect = cellRenderers[0].getOccupiedArea().getBBox();
canvas.moveTo(cellRect.getLeft(), cellRect.getBottom());
canvas.lineTo(cellRect.getLeft(), cellRect.getTop());
// vertical lines
for (int j = 0; j < cellRenderers.length; j++) {
cellRect = cellRenderers[j].getOccupiedArea().getBBox();
canvas.moveTo(cellRect.getRight(), cellRect.getBottom());
canvas.lineTo(cellRect.getRight(), cellRect.getTop());
}
}
canvas.stroke();
}
}

private class DottedLineCellRenderer extends CellRenderer {
public DottedLineCellRenderer(Cell modelElement) {
super(modelElement);
}

@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
drawContext.getCanvas().setLineDash(3f, 3f);
drawContext.getCanvas().rectangle(this.getOccupiedArea().getBBox());
drawContext.getCanvas().stroke();
}
}

分别。

<小时/>

正如 Alexey Subach 所解释的在 this answerRenderer 的完整自定义还应该覆盖 getNextRenderer() 方法。特别是当相关对象中可能发生区域中断时,这是必要的,否则定制只能在OP观察到的第一个区域中起作用。

关于java - itext在工作中如何找出坐标文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55960525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com