当你发现,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();
}
}
使用它是很容易的,只是一个实例绑定到该属性:
//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.HeaderRows
和table.FooterRows
。
玛丽亚的回答是最适合这种情况。对Chris Hass而言,他认为他的解决方案对于需要维护和更新的大型项目来说更好。他的解决方案是干的,而且这种方法更好。但如果快速和肮脏是好的,他们玛丽亚的方法是完美的。 – BillRuhl