如果我喜欢它: How during work itext find out coordinates text?
一切正常。但是,如果我在多一页上创建表,则不会绘制任何内容:
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, 0)));
table.addCell(new Cell().add(new Paragraph("test\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\ntest\n" )).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));
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();
}
}
我可以解决它吗?
答案 0 :(得分:1)
您正在使用的代码对实现如何在TableRenderer
内部进行工作有许多假设。
在您的代码中,您正在绘制虚线边框,我必须将您指向DashedBorder
类,您可以直接使用它:
Border customBorder = new DashedBorder(ColorConstants.RED, 2);
table.addCell(new Cell().add(new Paragraph("A2")).setBorder(customBorder));
您还可以通过以下方式实现您的自定义边框类:
private static class CustomBorder extends Border {
protected CustomBorder(Color color, float width) {
super(color, width);
}
@Override
public void draw(PdfCanvas canvas, float x1, float y1, float x2, float y2, Side defaultSide, float borderWidthBefore, float borderWidthAfter) {
}
@Override
public void drawCellBorder(PdfCanvas canvas, float x1, float y1, float x2, float y2, Side defaultSide) {
canvas.
saveState().
setStrokeColor(transparentColor.getColor());
transparentColor.applyStrokeTransparency(canvas);
canvas.
setLineDash(3, 3).
setLineWidth(width).
moveTo(x1, y1).
lineTo(x2, y2).
stroke().
restoreState();
}
@Override
public int getType() {
return Border.DASHED;
}
}
然后只创建一个实例并在setBorder
中使用它:
Border customBorder = new CustomBorder(ColorConstants.RED, 2);
table.addCell(new Cell().add(new Paragraph("A2")).setBorder(customBorder));