itext在工作期间如何找出坐标文本?

时间:2019-05-02 21:31:29

标签: java itext itext7

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

我只需要某些单词

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**

1 个答案:

答案 0 :(得分:2)

在iText7中,您可以使用自定义Renderer实现此类任务。该技术显示在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实现,以正常绘制文本。之后,您可以调用getOccupiedAreaBBox来检索用于绘制文本的区域,并将其用于您的任务,无论是用于装饰文本还是仅将位置存储在某处。


如果您想知道为什么此示例位于 events 子包中,那么该示例对应于一个iText5示例,在iText5中,您可以通过设置来实现此任务到正在讨论的Chunk的通用标记,并在页面事件侦听器的onGenericTag方法中侦听该通用标记,有关示例,请参见this answer


在您询问的评论中

  

我可以用桌子吗? (用帮助画布绘制边框)

是的,同样,您为此使用了关联的渲染器,例如参见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 answer所述,Renderer的完全自定义也应该覆盖getNextRenderer()方法。区域中断可能发生在所讨论的对象中,这是必要的,否则定制只能在OP观察到的第一个区域中起作用。