【问题标题】:iTextSharp table cell colspaniTextSharp 表格单元格跨度
【发布时间】:2018-08-21 11:51:52
【问题描述】:

我有一张桌子,一切正常,但只有一个问题。 即使我设置了正确的列,它也只是打印出每列的默认宽度......

在这种情况下,我尝试设置一个 float[](在实例化表格时)以查看是否可以设置每列的宽度(即使 Cell 对象的 colspan 应该已经处理了它),但是即便如此,它也没有改变。以前,我有列数的整数,但结果是一样的。我需要对此进行一些澄清,因为部分文档已过时。

doc.Add(NewTable(new float[4] { 0, 0, 0, 0 }, new float[3] { 1, 5, 5 }, 250, 0, -1, 200, 150,
                                        new List<Cell>() {
                                            NewCell(new Paragraph("N.º do Auto:").SetFont(FontBold), ColorGray, new SolidBorder(1),1, 1, 2.5f, HorizontalAlignment.LEFT,VerticalAlignment.MIDDLE),
                                            NewCell(new Paragraph("fdgdfgdfg").SetFont(FontRegular), ColorGray, new SolidBorder(1),1, 2, 2.5f, HorizontalAlignment.LEFT,VerticalAlignment.MIDDLE),
                                            NewCell(new Paragraph("Data:").SetFont(FontBold), ColorGray, Border.NO_BORDER,1, 1, 2.5f, HorizontalAlignment.LEFT,VerticalAlignment.MIDDLE),
                                            NewCell(new Paragraph(DateTime.Now.ToString("dd/MM/yyyy")).SetFont(FontRegular), ColorGray, Border.NO_BORDER,1, 2, 2.5f, HorizontalAlignment.LEFT,VerticalAlignment.MIDDLE),
                                            NewCell(new Paragraph("Morada:").SetFont(FontBold), ColorGray, Border.NO_BORDER,1, 1, 2.5f, HorizontalAlignment.LEFT,VerticalAlignment.MIDDLE),
                                            NewCell(new Paragraph("fdgdfgdfg").SetFont(FontRegular), ColorGray, Border.NO_BORDER,1, 2, 2.5f, HorizontalAlignment.LEFT,VerticalAlignment.MIDDLE)
                                        }));

/// <summary>
        /// Creates a new table
        /// </summary>
        /// <param name="paddings">An array of values to set the table's paddings (top, right, bottom, left).</param>
        /// <param name="pointColumnWidths">An array of values to specify the number of columns ocupied by each cell in the row.</param>
        /// <param name="width">The table's width.</param>
        /// <param name="rowStart">The table row from which to start writing.</param>
        /// <param name="rowEnd">The table row to which to end writing (-1 for all rows).</param>
        /// <param name="horizontalPosition">The table's horizontal position (from the bottom left).</param>
        /// <param name="verticalPosition">The table's vertical position (from the bottom left).</param>
        /// <param name="cells">A list of cells to be added to the table.</param>
        /// <returns></returns>
        private static Table NewTable(float[] paddings, float[] pointColumnWidths, float width, int rowStart, int rowEnd, float horizontalPosition, float verticalPosition, List<Cell> cells)
        {
            Table table = new Table(pointColumnWidths);

            table.SetPaddings(paddings[0], paddings[1], paddings[2], paddings[3]);

            foreach (Cell cell in cells)
                table.AddCell(cell);

            table.SetFixedPosition(horizontalPosition, verticalPosition, width);

            return table;
        }

        /// <summary>
        /// Creates a new cell
        /// </summary>
        /// <param name="paragraph">A Paragraph object with the content.</param>
        /// <param name="backgroundColor">The background color of the cell.</param>
        /// <param name="border">The border to be applied to the cell.</param>
        /// <param name="rowSpan">The number of rows that the cell will ocupy in the table.</param>
        /// <param name="colSpan">The number of columns that the cell will ocupy in the table.</param>
        /// <param name="padding">The padding of the cell.</param>
        /// <param name="horizontalAlignment">The enum value for the horizontal alignment. Ex: HorizontalAlignment.LEFT</param>
        /// <param name="verticalAlignment">The enum value for the vertical alignment. Ex: VerticalAlignment.MIDDLE</param>
        /// <returns></returns>
        private static Cell NewCell(Paragraph paragraph, Color backgroundColor, Border border, int rowSpan, int colSpan, float padding, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
        {
            Cell cell = new Cell(rowSpan, colSpan);
            cell.SetBackgroundColor(backgroundColor);
            cell.SetHorizontalAlignment(horizontalAlignment);
            cell.SetVerticalAlignment(verticalAlignment);
            cell.SetPadding(padding);
            cell.SetBorder(border);
            cell.Add(paragraph);

            return cell;
        }

【问题讨论】:

    标签: c# itext


    【解决方案1】:

    你传递了float[] pointColumnWidths,但它没有在任何地方使用。

    试试这个:

    private static Table NewTable(float[] paddings, float[] pointColumnWidths, float width, int rowStart, int rowEnd, float horizontalPosition, float verticalPosition, List<Cell> cells)
    {
        Table table = new Table(pointColumnWidths);
    
        table.SetPaddings(paddings[0], paddings[1], paddings[2], paddings[3]);
    
        foreach (Cell cell in cells)
            table.AddCell(cell);
    
        table.SetFixedPosition(horizontalPosition, verticalPosition, width);
    
        return table;
    }
    

    【讨论】:

    • 谢谢。问题在于百分比。从他们的示例中,我认为它的工作方式类似于 bootstrap 中的 colspan,其中您总共有 12 列。不再有 SetTotalWidth 方法。我正在使用版本 7.1.2。不过,这是我的错。我在 Table 构造函数中有 float[] pointColumnWidths 。当我尝试查看问题所在时,我忘记纠正了。
    • 所以,我已经在我的主要帖子中更正了它。也修改你的,这样我就可以接受你的答案。我再次感谢。 ;)
    • @taiko,谢谢指导,代码已更改
    • 不客气。顺便说一句,我刚刚意识到 float[] 与百分比无关。这是关于桌子的宽度。例如,如果您有一个宽度为 300 的表格,则数组中浮点值的总和必须是表格的总和(或者其中一个单元格将非常大,哈哈)。在您的示例中,您将填充 100 个点和另外 200 个空点,其中一个单元格必须填充。
    • itext 中的表 api 从早期的 7.0.x 到现在的 7.1.x 发生了很大的变化。这甚至可能导致以前参数为百分比的方法将它们解释为绝对宽度。
    猜你喜欢
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多