iTextSharp表单元格colspan

时间:2018-08-21 11:51:52

标签: c# itext

我有一张桌子,所有内容都可以打印出来,但是只有一个问题。 即使我将列设置正确,它也只是为每列打印出默认宽度...

在这种情况下,我尝试设置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;
        }

1 个答案:

答案 0 :(得分:2)

您通过了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;
}