gpt4 book ai didi

java - 在 itext7 中为跨越多个页面的表格绘制自定义边框

转载 作者:行者123 更新时间:2023-12-02 09:54:35 27 4
gpt4 key购买 nike

如果我确实喜欢它: 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();
}
}

我可以修复它吗?

enter image description here

enter image description here

最佳答案

您使用的代码对 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));

关于java - 在 itext7 中为跨越多个页面的表格绘制自定义边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56092142/

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