如何在下一行 c# 中显示 pdftable(iTextSharp) 的单元格(列及其行值)?

How to show a cell(column with its row values) of a pdftable(iTextSharp) in next line c#?

我正在生成带有 PdfPTable 的 PDF 文档。共有三列,例如 Col aCol bCol c。目前它看起来像这样:

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

但是由于 Col c 是描述,它包含很多数据,我希望 table 看起来像这样:

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));
 }

请查看 SimpleTable9 示例。 它创建如下屏幕截图所示的 PDF:

如果您希望所有数据都在一行中,您可以更改列的宽度并确保第三列比前两行有更多的空间。

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。

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);