如何在 C# 中使用 itextsharp 对齐添加到单个 table 的多个 table?
How to align multiple tables added to a single table using itextsharp in c#?
我创建了一个有 3 列的 table 和另一个有 6 列的 table,然后将其添加到另一个 table 以使其成为一个 table。我想对齐 3 列的第二列 table 和 6 列的第二列 table 像这样:
谁能告诉我如何对齐 2 个不同 table 的第二列?
我正在使用 iTextsharp 创建 tables.
有多种方法可以做到这一点。我会解释两个。
两个 table 具有对齐的列宽
设置列宽以获得所需的对齐方式。
// Table with 3 columns
PdfPTable table1 = new PdfPTable(3);
table1.SetTotalWidth(new float[] { 50, 10, 300 });
table1.AddCell("One");
table1.AddCell(" ");
table1.AddCell(" ");
// Table with 6 columns
PdfPTable table2 = new PdfPTable(6);
// Column 1 and 2 are the same widths as those of table1
// Width of columns 3-6 sum up to the width of column 3 of table1
table2.SetTotalWidth(new float[] { 50, 10, 120, 50, 10, 120 });
for (int row = 0; row < 2; row++)
{
for (int column = 0; column < 6; column++)
{
table2.AddCell(" ");
}
}
doc.Add(table1);
doc.Add(table2);
... 外层 table
您提到在另一个 table 中添加两个 table。如果这是一个明确的要求,则可能:
// Outer table with 1 column
PdfPTable outer = new PdfPTable(1);
// create table1 and table2 like in the previous example
// Add both tables to the outer table
outer.AddCell(new PdfPCell(table1));
outer.AddCell(new PdfPCell(table2));
doc.Add(outer);
视觉效果同上
使用 colspans
与其考虑两个单独的 table,您还可以考虑这个 table,其中一些单元格跨越多个列。
// Table with 6 columns
PdfPTable table = new PdfPTable(6);
table.SetWidths(new float[] { 50, 10, 120, 50, 10, 120 });
table.AddCell("Two");
table.AddCell(" ");
// Third cell on the first row has a colspan of 4
PdfPCell cell = new PdfPCell();
cell.Colspan = 4;
table.AddCell(cell);
// Second and third row have 6 cells
for (int row = 0; row < 2; row++)
{
for (int column = 0; column < 6; column++)
{
table.AddCell(" ");
}
}
doc.Add(table);
这种方法的好处是您不必 fiddle 保持多个 table 之间的列宽一致。因为这是一个 table,列将始终对齐。
我创建了一个有 3 列的 table 和另一个有 6 列的 table,然后将其添加到另一个 table 以使其成为一个 table。我想对齐 3 列的第二列 table 和 6 列的第二列 table 像这样:
谁能告诉我如何对齐 2 个不同 table 的第二列? 我正在使用 iTextsharp 创建 tables.
有多种方法可以做到这一点。我会解释两个。
两个 table 具有对齐的列宽
设置列宽以获得所需的对齐方式。
// Table with 3 columns
PdfPTable table1 = new PdfPTable(3);
table1.SetTotalWidth(new float[] { 50, 10, 300 });
table1.AddCell("One");
table1.AddCell(" ");
table1.AddCell(" ");
// Table with 6 columns
PdfPTable table2 = new PdfPTable(6);
// Column 1 and 2 are the same widths as those of table1
// Width of columns 3-6 sum up to the width of column 3 of table1
table2.SetTotalWidth(new float[] { 50, 10, 120, 50, 10, 120 });
for (int row = 0; row < 2; row++)
{
for (int column = 0; column < 6; column++)
{
table2.AddCell(" ");
}
}
doc.Add(table1);
doc.Add(table2);
... 外层 table
您提到在另一个 table 中添加两个 table。如果这是一个明确的要求,则可能:
// Outer table with 1 column
PdfPTable outer = new PdfPTable(1);
// create table1 and table2 like in the previous example
// Add both tables to the outer table
outer.AddCell(new PdfPCell(table1));
outer.AddCell(new PdfPCell(table2));
doc.Add(outer);
视觉效果同上
使用 colspans
与其考虑两个单独的 table,您还可以考虑这个 table,其中一些单元格跨越多个列。
// Table with 6 columns
PdfPTable table = new PdfPTable(6);
table.SetWidths(new float[] { 50, 10, 120, 50, 10, 120 });
table.AddCell("Two");
table.AddCell(" ");
// Third cell on the first row has a colspan of 4
PdfPCell cell = new PdfPCell();
cell.Colspan = 4;
table.AddCell(cell);
// Second and third row have 6 cells
for (int row = 0; row < 2; row++)
{
for (int column = 0; column < 6; column++)
{
table.AddCell(" ");
}
}
doc.Add(table);
这种方法的好处是您不必 fiddle 保持多个 table 之间的列宽一致。因为这是一个 table,列将始终对齐。