在PDF中的另一行中创建一个长单元格

时间:2014-08-08 07:48:43

标签: itextsharp

我有一个包含5列的iTextSharp表。在每行5个单元格之后,我想在另一行中添加另一个单元格,只有一列(因为这个单元格的长度太长)。

我试图添加第6个单元格,但布局不是我想要的,

就像: 第一行:记录1的5个单元格 第二行:记录1的第6行 线(BORDER) 第3行:记录2的5个单元格 第4行:记录2的第6个单元格   这是我的代码

it.Font tableCellFont = new it.Font(testBaseFont, 7, it.Font.NORMAL, it.Color.BLACK);
it.Font tableCellBoldFont = new it.Font(testBaseFont, 7, it.Font.BOLD, it.Color.BLACK);
it.Font tableHeaderFont = new it.Font(testBaseFont, 8, it.Font.BOLD, it.Color.BLACK);

//Create new PDF document
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 = it.Table.TOP_BORDER | it.Table.BOTTOM_BORDER;
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("Name", tableHeaderFont));
logTable.AddCell(new it.Phrase("Country", tableHeaderFont));
logTable.AddCell(new it.Phrase("City", tableHeaderFont));
logTable.AddCell(new it.Phrase("Job", tableHeaderFont));
logTable.AddCell(new it.Phrase("Age", tableHeaderFont));

logTable.AddCell(new it.Cell(new it.Phrase("Martin", tableCellFont)));
logTable.AddCell(new it.Phrase("Italy", tableCellFont));
logTable.AddCell(new it.Phrase("Torino", tableCellFont));
logTable.AddCell(new it.Phrase("Software developer", tableCellFont));
logTable.AddCell(new it.Phrase("29", tableCellFont));
//here I'd like to insert a row


logTable.AddCell(new it.Cell(new it.Phrase("Jack", tableCellFont)));
logTable.AddCell(new it.Phrase("U.S.A", tableCellFont));
logTable.AddCell(new it.Phrase("New York", tableCellFont));
logTable.AddCell(new it.Phrase("Manager", tableCellFont));
logTable.AddCell(new it.Phrase("50", tableCellFont));

//here I'd like to insert a row

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

1 个答案:

答案 0 :(得分:1)

(对于未来的读者,我必须在上一个问题中加入相同的警告。)

Table不再支持,您使用的是iTextSharp的版本,您应该切换到PdfPTable

但是,您要做的是在单元格上设置要跨越多列的ColSpan

var t = new iTextSharp.text.Table(5);
t.AddCell("A");
t.AddCell("B");
t.AddCell("C");
t.AddCell("D");
t.AddCell("E");

t.AddCell(new Cell("This is my long text") { Colspan = 5 });

或者,如果您使用的是旧版本的C#:

var c = new Cell("This is my long text");
c.Colspan = 5;
t.AddCell(c);

修改

要更改边框,最简单的方法就是在需要时更改DefaultCell属性。

//Turn off borders
t.DefaultCell.Border = Table.NO_BORDER;
t.AddCell("A");
t.AddCell("B");
t.AddCell("C");
t.AddCell("D");
t.AddCell("E");

//Turn bottom borders on
t.DefaultCell.Border = Table.BOTTOM_BORDER;
t.AddCell(new Cell("This is my long text") { Colspan = 5 });

//Turn borders off again
t.DefaultCell.Border = Table.NO_BORDER;
t.AddCell("F");
t.AddCell("G");
t.AddCell("H");
t.AddCell("I");
t.AddCell("J");