我正在使用PdfPTable
生成PDF文档。有三列说 Col a , Col b 和 Col c 。目前它看起来像这样:
Col a | Col b | Col c
--------------------------------------------------------
ValuesofCol a | ValuesofCol b | ValuesofCol c
但由于 Col 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 :(得分:1)
请查看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 = 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);