C# 和 iText7,以绝对值设置列宽

C# and iText7, set column width in absolute values

我必须在 c# 中使用 iText7 库实现如下内容:

我用下面的代码实现了它:

// doc is the document created in the Main function
public static void CreatePdf(Document doc) {
            // Initialize document
            Document document = doc;

            // a float array for columns width
            float[] colwidths = { 1, 3 };

            // creates table with a number of col equals to how many numbers are in float array
            Table table = new Table(UnitValue.CreatePercentArray(colwidths) );
            table.SetWidth(523);

            table.AddCell(new Cell(1,2).Add(new Paragraph("The table title goes here")));

            // creates a cell that is 3rows and 1col large; vertically centers the text
            table.AddCell(new Cell(3, 1).Add(new Paragraph("here goes the Text1"))
                 .SetVerticalAlignment(VerticalAlignment.MIDDLE) );

            // adds 3 rows in the 2nd column
            table.AddCell("row 1");
            table.AddCell("row 2");
            table.AddCell("row 3");

            document.Add(table);
            document.Close();
        }

这样我决定第二列的宽度是第一列的 3 倍。

但我想用绝对值来做到这一点,所以我可以更精确地添加列和行,I.E.我知道总宽度是 523 点,所以我希望第一列宽 123 点,第二列宽 400 点。

Table 有一个接受 float[] 的构造函数,您可以在其中指定以磅为单位的列宽:

Table table = new Table(new float[] { 123, 400 });

生成的 table 宽度将是所提供列的总和,除非您明确设置宽度(使用 SetWidth),在这种情况下,每列都会按比例调整大小。

例如,如果您调用:

table.SetWidth(1046);

生成的列宽将分别加倍至 226 点和 800 点。