gpt4 book ai didi

c# - Itextsharp PDFPTable 如何在整个表格周围制作边框

转载 作者:行者123 更新时间:2023-11-30 14:30:04 26 4
gpt4 key购买 nike

我正在使用 PDFPTable 通过 Itextsharp 中的数据库构建表格,要求表格中的行/单元格没有顶部或底部底部边框,但每个单元格的左侧和右侧都有黑色边框(换句话说,每列都有一个左右边框),表格的底部也需要用黑色边框关闭,这就是我的问题所在。

我正在做的是将边框设置为 0,然后手动分配边框,这样我就只能在每个单元格上获得左右边框,如下所示,这是生成“数量”列的示例:

    cell = new PdfPCell(new Phrase(Qty.value, subheaderFont));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.BackgroundColor = new iTextSharp.text.BaseColor(220, 220, 220);
cell.Border = 0;
cell.BorderColorLeft = BaseColor.BLACK;
cell.BorderWidthLeft = .5f;
cell.BorderColorRight = BaseColor.BLACK;
cell.BorderWidthRight = .5f;
table.AddCell(cell);

问题显然是我无法检测最后一行以添加边框底部,但我想必须有一种方法来控制“表格”本身的边框,或者我采取了错误的方法到这个?

最佳答案

如您所见,PdfPTable没有边框,可能是因为 PDF 一开始就没有表格。将边框放在 PdfPCell 上可能更有意义直接(即使 PDF 也不支持这些)。无论如何,表格只是单元格的集合,所以让他们处理它。

无论如何,解决方案是在 PdfPTable 类的实例上设置 TableEvent。为此,您需要自定义实现 IPdfPTableEvent界面。下面的代码通常应该为您执行此操作(请参阅底部的“一般”注释)

class TopBottomTableBorderMaker : IPdfPTableEvent {
private BaseColor _borderColor;
private float _borderWidth;

/// <summary>
/// Add a top and bottom border to the table.
/// </summary>
/// <param name="borderColor">The color of the border.</param>
/// <param name="borderWidth">The width of the border</param>
public TopBottomTableBorderMaker(BaseColor borderColor, float borderWidth ) {
this._borderColor = borderColor;
this._borderWidth = borderWidth;
}
public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
//widths (should be thought of as x's) is an array of arrays, first index is for each row, second index is for each column
//The below uses first and last to calculate where each X should start and end
var firstRowWidths = widths[0];
var lastRowWidths = widths[widths.Length - 1];

var firstRowXStart = firstRowWidths[0];
var firstRowXEnd = firstRowWidths[firstRowWidths.Length - 1] - firstRowXStart;

var lastRowXStart = lastRowWidths[0];
var lastRowXEnd = lastRowWidths[lastRowWidths.Length - 1] - lastRowXStart;

//heights (should be thought of as y's) is the y for each row's top plus one extra for the last row's bottom
//The below uses first and last to calculate where each Y should start and end
var firstRowYStart = heights[0];
var firstRowYEnd = heights[1] - firstRowYStart;

var lastRowYStart = heights[heights.Length - 1];
var lastRowYEnd = heights[heights.Length - 2] - lastRowYStart;

//Where we're going to draw our lines
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];

//I always try to save the previous state before changinge anything
canvas.SaveState();

//Set our line properties
canvas.SetLineWidth(this._borderWidth);
canvas.SetColorStroke(this._borderColor);

//Draw some rectangles
canvas.Rectangle(
firstRowXStart,
firstRowYStart,
firstRowXEnd,
firstRowYEnd
);
//They aren't actually drawn until you stroke them!
canvas.Stroke();

canvas.Rectangle(
lastRowXStart,
lastRowYStart,
lastRowXEnd,
lastRowYEnd
);
canvas.Stroke();

//Restore any previous settings
canvas.RestoreState();

}
}

使用起来非常简单,只需将一个实例绑定(bind)到属性上即可:

//Create your name as you normally do
var table = new PdfPTable(3);

//Bind and instance with properties set
table.TableEvent = new TopBottomTableBorderMaker(BaseColor.BLACK, 0.5f);

//The rest is the same
for (var i = 0; i < 6; i++) {
var cell = new PdfPCell(new Phrase(i.ToString()));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.BackgroundColor = new iTextSharp.text.BaseColor(220, 220, 220);
cell.Border = 0;
cell.BorderColorLeft = BaseColor.BLACK;
cell.BorderWidthLeft = .5f;
cell.BorderColorRight = BaseColor.BLACK;
cell.BorderWidthRight = .5f;
table.AddCell(cell);
}

上面我说“一般”它应该有效。但是,如果您有表格页眉和/或页脚,您也需要将它们考虑在内。这应该不会太难,但您需要调整 y 值以考虑 table.HeaderRowstable.FooterRows

关于c# - Itextsharp PDFPTable 如何在整个表格周围制作边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24540129/

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