2015-05-04 48 views
0

我正在生成一個帶有PdfPTable的PDF文檔。有三欄說列a,列b列c。目前,它看起來來像這樣:如何在下一行c#中顯示pdftable(iTextSharp)的單元格(具有其行值的列)?

Col a   | Col b   | Col c 
-------------------------------------------------------- 
ValuesofCol a | ValuesofCol b | ValuesofCol c 

但隨着山口Ç是說明,它包含了大量的數據,我想表看起來像這樣:

Col a      | Col b 
-------------------------------------------------------- 
ValueofCol a    | ValueofColb 
-------------------------------------------------------- 
Col c 
-------------------------------------------------------- 
ValueOfCol c 
-------------------------------------------------------- 
Cola      | ColB 
-------------------------------------------------------- 
ValueofCol a    | ValueofCol b 
-------------------------------------------------------- 
Colc 
-------------------------------------------------------- 
ValueOfCol c 
-------------------------------------------------------- 

這裏我寫的代碼片段

PdfPTable table = new PdfPTable(3); 
table.HorizontalAlignment = 0; 
table.SpacingBefore = 10f; 
table.SpacingAfter = 10f; 
table.TotalWidth = 550f; 
table.LockedWidth = true; 
float[] widths = new float[] { 2f, 3f, 3f }; 
table.SetWidths(widths); 

//Header 
table.AddCell(CreateHeaderCell("Cola")); 
table.AddCell(CreateHeaderCell("Colb ")); 
table.AddCell(CreateHeaderCell("Colc")); 

//Row 1 
if (DsetAssesmentSummary.Tables[0].Rows.Count > 0) 
{ 
    table.AddCell(CreateCell(DsetAssesmentSummary.Tables[0].Rows[0]["Cola"].ToString(), true)); 
} 
if (DsetAssesmentSummary.Tables[0].Rows.Count > 0) 
{ 
    table.AddCell(CreateCell(DsetAssesmentSummary.Tables[0].Rows[0]["Colb"].ToString(), true)); 
} 
if (DsetAssesmentSummary.Tables[0].Rows.Count > 0) 
{ 
    table.AddCell(CreateCell(DsetAssesmentSummary.Tables[0].Rows[0]["Colc"].ToString(), true)); 
} 
+0

看看你有什麼以下'山口C'所以也許它應該被定義爲'Colc' – MethodMan

+0

嗨MethodMan-感謝烏拉圭回合的反應,我給了一個虛擬的列名作爲COLC。布魯諾的文章幫助我解決了問題 –

回答

1

請看看SimpleTable9的例子。 它會在下面的屏幕截圖顯示的PDF:

enter image description here

如果你想在單行中的所有數據,你可以改變列的寬度,並確保第三欄有更多的地方比前兩行。

PdfPTable table = new PdfPTable(3); 
table.setSpacingBefore(5); 
table.setWidths(new int[]{1, 1, 8}); 
table.setWidthPercentage(100); 
table.addCell("Col a"); 
table.addCell("Col b"); 
table.addCell("Col c"); 
table.addCell("Value a"); 
table.addCell("Value b"); 
table.addCell("This is a long description for column c. It needs much more space hence we made sure that the third column is wider."); 
document.add(table); 

如果你只想要兩列,那麼你需要創建一個只有兩列的表。

table = new PdfPTable(2); 
table.setSpacingBefore(5); 
table.setWidthPercentage(100); 
table.getDefaultCell().setColspan(1); 
table.addCell("Col a"); 
table.addCell("Col b"); 
table.addCell("Value a"); 
table.addCell("Value b"); 
table.getDefaultCell().setColspan(2); 
table.addCell("Value b"); 
table.addCell("This is a long description for column c. It needs much more space hence we made sure that the third column is wider."); 
table.getDefaultCell().setColspan(1); 
table.addCell("Col a"); 
table.addCell("Col b"); 
table.addCell("Value a"); 
table.addCell("Value b"); 
table.getDefaultCell().setColspan(2); 
table.addCell("Value b"); 
table.addCell("This is a long description for column c. It needs much more space hence we made sure that the third column is wider."); 
document.add(table); 
+0

大家好,非常感謝,對我來說工作得很好 –

+0

謝謝你布魯諾,我已經標記你的建議作爲答案。它工作正常.. –

相關問題