2014-04-05 24 views
0

我正在向pdf中添加一個表格。我有3行3列。我希望第一列只出現一次,作爲所有行的單個單元格。我該怎麼做?我的代碼如下。我的輸出應該來像德勤公司的列顯示的圖像:多列的恆定列

enter image description here

 PdfPTable table = new PdfPTable(dt.Columns.Count); 
     PdfPRow row = null; 
     float[] widths = new float[] { 4f, 4f, 4f }; 
     table.SetWidths(widths); 
     table.WidthPercentage = 100; 
     int iCols = 0; 
     string colname = ""; 
     PdfPCell cells = new PdfPCell(new Phrase("Products")); 
     cells.Colspan = dt.Columns.Count; 

     foreach (DataColumn c in dt.Columns) 
     { 

      table.AddCell(new Phrase(c.ColumnName, fontbold)); 
     } 

     foreach (DataRow r in dt.Rows) 
     { 
      if (dt.Rows.Count > 0) 
      { 
       table.AddCell(new Phrase(r[0].ToString(), font5)); 
       table.AddCell(new Phrase(r[1].ToString(), font5)); 
       table.AddCell(new Phrase(r[2].ToString(), font5)); 
      } 
     } document.Add(table); 
+0

你在使用什麼庫?將它添加爲標籤,同時將PDF添加到您的問題中,因爲它現在聽起來很像DB。 – TaW

+0

您是否在'PdfPCell'上嘗試過'RowSpan'? –

回答

1

MyFirstTable例如,從我的書不正是你需要的。移植到C#,它看起來像這樣:

PdfPTable table = new PdfPTable(3); 
// the cell object 
PdfPCell cell; 
// we add a cell with colspan 3 
cell = new PdfPCell(new Phrase("Cell with colspan 3")); 
cell.Colspan = 3; 
table.AddCell(cell); 
// now we add a cell with rowspan 2 
cell = new PdfPCell(new Phrase("Cell with rowspan 2")); 
cell.Rowspan = 2; 
table.AddCell(cell); 
// we add the four remaining cells with addCell() 
table.AddCell("row 1; cell 1"); 
table.AddCell("row 1; cell 2"); 
table.AddCell("row 2; cell 1"); 
table.AddCell("row 2; cell 2"); 

你可以看一下生成的PDF here。在你的情況下,你需要

cell.Rowspan = 6; 

對於價值德勤的單元格。