在PDF的行中添加行

时间:2014-08-07 14:44:30

标签: itextsharp

我的代码就是这样,这里itITextSharp.Text

我想问一下,如果我想在每一行之间添加黑线,该怎么做? 我试图找到ITEXTSharp.Text.Table的属性,但我找不到那里的任何属性。

it.Document doc = new it.Document();
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create));        
doc.Open();
it.Phrase p = new it.Phrase("item1", new it.Font(testBaseFont, 14, it.Font.BOLD, it.Color.BLACK));
p = new it.Phrase("item2", new it.Font(testBaseFont, 14, it.Font.BOLD, it.Color.BLACK));
p.Leading = 2;
doc.Add(p);
it.Table logTable = new it.Table(5);
logTable.Alignment = it.Table.ALIGN_LEFT;
logTable.DefaultVerticalAlignment = it.Element.ALIGN_MIDDLE;
logTable.Width = 100;
logTable.Border = it.Table.TOP_BORDER | it.Table.BOTTOM_BORDER;
logTable.BorderWidth = 0.5f;
logTable.DefaultCellBorder = 0;
logTable.Cellspacing = 2;
logTable.Cellpadding = 0;
logTable.DefaultCellBackgroundColor = it.Color.WHITE;
logTable.SetWidths(new int[] { 20, 20, 20, 20, 20 });
//Write column headers
logTable.AddCell(new it.Phrase("Column1", tableHeaderFont));
logTable.AddCell(new it.Phrase("Column2", tableHeaderFont));
logTable.AddCell(new it.Phrase("Column3", tableHeaderFont));
logTable.AddCell(new it.Phrase("Column4", tableHeaderFont));
logTable.AddCell(new it.Phrase("Column5", tableHeaderFont));

logTable.AddCell(new it.Cell(new it.Phrase("1", tableCellFont)));
logTable.AddCell(new it.Phrase("12", tableCellFont));
logTable.AddCell(new it.Phrase("13", tableCellFont));
logTable.AddCell(new it.Phrase("14", tableCellFont));
logTable.AddCell(new it.Phrase("15", tableCellFont));


logTable.AddCell(new it.Cell(new it.Phrase("2", tableCellFont)));
logTable.AddCell(new it.Phrase("22", tableCellFont));
logTable.AddCell(new it.Phrase("23", tableCellFont));
logTable.AddCell(new it.Phrase("24", tableCellFont));
logTable.AddCell(new it.Phrase("25", tableCellFont));

doc.Add(logTable);
doc.Close();

1 个答案:

答案 0 :(得分:2)

首先,Table deprecated many years ago支持PdfPTable,因此您希望切换到该版本。

然后只需要更改表实例的DefaultCell

var t = new PdfPTable(2);
//Use only a bottom border
t.DefaultCell.Border = PdfPCell.BOTTOM_BORDER;
for (var i = 0; i < 10; i++) {
    t.AddCell("Hello");
    t.AddCell(i.ToString());
}

如果您仍然需要完整的表格边框,但只需要行的超厚底部边框,您也可以这样做:

var t = new PdfPTable(2);
//Use a thicker border for the bottom
t.DefaultCell.BorderWidthBottom = 2;
for (var i = 0; i < 10; i++) {
    t.AddCell("Hello");
    t.AddCell(i.ToString());
}