代码之家  ›  专栏  ›  技术社区  ›  taiko

ITextSharp表格单元格Colspan

  •  0
  • taiko  · 技术社区  · 6 年前

    我有一张桌子,所有东西都印好了,但只有一个问题。 即使我将列设置正确,它也只打印出每列的默认宽度…

    在这种情况下,我尝试设置一个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 回复  |  直到 6 年前
        1
  •  2
  •   saAction    6 年前

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