Search code examples
c#itextitext7

Why is my iText7 table not designed correctly?


I am using iText7 to create an Excel-like table in a pdf document.

The problem is that when I design my table and add the columns to it, it is not lining up the columns and rows correctly. It looks like:

enter image description here

When it should be looking like:

enter image description here

I want each row to have the same columns widths.

This is the code that I have been using for creating the table:

public MemoryStream ExpMovimientosKardexPDF(MovimientoKardexRequest request)
{
    PaginationResponse<MovimientoKardexModel> response = MovimientosKardex(request);

    MemoryStream results = new()
    {
        Position = 0
    };

    //INICIALIZAR DOCUMENTO
    PdfWriter pdfWriter = new PdfWriter(results);
    PdfDocument pdfDocument = new PdfDocument(pdfWriter);
    Document document = new Document(pdfDocument);

    document.GetPdfDocument().SetDefaultPageSize(PageSize.A4.Rotate());
    //EMPRESA
    EmpresaEntity empresa = _context.Empresas.Where(x => x.Id == SecurityContext.CompanyId).First();
    Paragraph headerWHM = new Paragraph($"{empresa.NombreComercial}")
        .SetTextAlignment(TextAlignment.CENTER)
        .SetFontSize(20)
        .SetBold();
    document.Add(headerWHM);

    //TIPO DE REPORTE
    Paragraph headerKardex = new Paragraph("MOVIMIENTOS KARDEX")
        .SetTextAlignment(TextAlignment.CENTER)
        .SetFontSize(20)
        .SetBold();
    document.Add(headerKardex);

    //FECHA EN EL TITULO
    Paragraph headerFecha = new Paragraph($"Fecha reporte: {DateOnly.FromDateTime(DateTime.Now)} - {TimeOnly.FromDateTime(DateTime.Now)}")
        .SetTextAlignment(TextAlignment.CENTER)
        .SetFontSize(20)
        .SetBold();
    document.Add(headerFecha);

    //TABLA
    Table tabla = new Table(19, true)
        .SetWidth(UnitValue.CreatePercentValue(100))
        .SetFontSize(10);
    iText.Kernel.Colors.Color color = new iText.Kernel.Colors.DeviceRgb(160, 12, 4);

    Cell cellFecha = new Cell(1, 1)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("FECHA"));
    tabla.AddCell(cellFecha);

    Cell cellTipoMovimiento = new Cell(1, 2)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("TIPO DE MOVIMIENTO"));
    tabla.AddCell(cellTipoMovimiento);

    Cell cellMovimiento = new Cell(1, 3)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("MOVIMIENTO"));
    tabla.AddCell(cellMovimiento);

    Cell cellSucursal = new Cell(1, 4)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("SUCURSAL"));
    tabla.AddCell(cellSucursal);

    Cell cellAlmacen = new Cell(1, 5)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("ALMACEN"));
    tabla.AddCell(cellAlmacen);

    Cell cellPrioridad = new Cell(1, 6)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("PRIORIDAD"));
    tabla.AddCell(cellPrioridad);

    Cell cellClienteDestino = new Cell(1, 7)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("CLIENTE DESTINO"));
    tabla.AddCell(cellClienteDestino);

    Cell cellUsuario = new Cell(1, 8)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("USUARIO"));
    tabla.AddCell(cellUsuario);

    Cell cellClaveSKU = new Cell(1, 9)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("CLAVE/SKU"));
    tabla.AddCell(cellClaveSKU);

    Cell cellDescripcion = new Cell(1, 10)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("DESCRIPCION"));
    tabla.AddCell(cellDescripcion);

    Cell cellLote = new Cell(1, 11)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("LOTE"));
    tabla.AddCell(cellLote);

    Cell cellCantidad = new Cell(1, 12)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("CANTIDAD"));
    tabla.AddCell(cellCantidad);

    Cell cellCostoUnitario = new Cell(1, 13)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("COSTO UNITARIO"));
    tabla.AddCell(cellCostoUnitario);

    Cell cellCostoTotal = new Cell(1, 14)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("COSTO TOTAL"));
    tabla.AddCell(cellCostoTotal);

    Cell cellBultos = new Cell(1, 15)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("BULTOS"));
    tabla.AddCell(cellBultos);

    Cell cellTarima = new Cell(1, 16)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("TARIMA"));
    tabla.AddCell(cellTarima);

    Cell cellFactura = new Cell(1, 17)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("FACTURA"));
    tabla.AddCell(cellFactura);

    Cell cellCliente = new Cell(1, 18)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("CLIENTE"));
    tabla.AddCell(cellCliente);

    Cell cellDocumento = new Cell(1, 19)
        .SetBackgroundColor(color)
        .SetFontColor(iText.Kernel.Colors.ColorConstants.WHITE)
        .SetBorder(new SolidBorder(4))
        .SetBold()
        .Add(new Paragraph("DOCUMENTO"));
    tabla.AddCell(cellDocumento);

    int recordIndex = 2;
    decimal? totalCantidad =0, totalCostoU=0, totalCostoT=0, totalBultos=0, totalTarimas = 0;
    foreach(MovimientoKardexModel item in response.Results)
    {
        Cell cellValorFecha = new Cell(recordIndex, 1)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Fecha.ToString()));
        tabla.AddCell(cellValorFecha);

        Cell cellValorTipoMovimiento = new Cell(recordIndex, 2)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Movimiento));
        tabla.AddCell(cellValorTipoMovimiento);

        Cell cellValorMovimiento = new Cell(recordIndex, 3)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.IdMovimiento.ToString()));
        tabla.AddCell(cellValorMovimiento);

        Cell cellValorSucursal = new Cell(recordIndex, 4)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Sucursal));
        tabla.AddCell(cellValorSucursal);

        Cell cellValorAlmacen = new Cell(recordIndex, 5)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Sucursal));
        tabla.AddCell(cellValorAlmacen);

        Cell cellValorPrioridad = new Cell(recordIndex, 6)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Prioridad));
        tabla.AddCell(cellValorPrioridad);

        Cell cellValorClienteDestino = new Cell(recordIndex, 7)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.ClienteDestino));
        tabla.AddCell(cellValorClienteDestino);

        Cell cellValorUsuario = new Cell(recordIndex, 8)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Usuario));
        tabla.AddCell(cellValorUsuario);

        Cell cellValorClaveSKU = new Cell(recordIndex, 9)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Clave));
        tabla.AddCell(cellValorClaveSKU);

        Cell cellValorDescripcion = new Cell(recordIndex, 10)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Descripcion));
        tabla.AddCell(cellValorDescripcion);

        Cell cellValorLote = new Cell(recordIndex, 11)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(""));
        tabla.AddCell(cellValorLote);

        Cell cellValorCantidad = new Cell(recordIndex, 12)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Cantidad.ToString()));
        tabla.AddCell(cellValorCantidad);

        Cell cellValorCostoUnitario = new Cell(recordIndex, 13)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.CostoUnitario.ToString()));
        tabla.AddCell(cellValorCostoUnitario);

        Cell cellValorCostoTotal = new Cell(recordIndex, 14)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.CostoTotal.ToString()));
        tabla.AddCell(cellValorCostoTotal);

        Cell cellValorBultos = new Cell(recordIndex, 15)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.CostoUnitario.ToString()));
        tabla.AddCell(cellValorBultos);

        Cell cellValorTarima = new Cell(recordIndex, 16)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Tarimas.ToString()));
        tabla.AddCell(cellValorTarima);

        Cell cellValorFactura = new Cell(recordIndex, 17)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Factura));
        tabla.AddCell(cellValorFactura);

        Cell cellValorCliente = new Cell(recordIndex, 18)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Cliente));
        tabla.AddCell(cellValorCliente);

        Cell cellValorDocumento = new Cell(recordIndex, 19)
            .SetBorder(new SolidBorder(2))
            .Add(new Paragraph(item.Documento));
        tabla.AddCell(cellValorDocumento);

        totalCantidad = totalCantidad + item.Cantidad;
        totalCostoU = totalCostoU + item.CostoUnitario;
        totalCostoT = totalCostoT + item.CostoTotal;
        totalBultos = totalBultos + item.Bultos;
        totalTarimas = totalTarimas + item.Tarimas;

        recordIndex++;
    }

    document.Add(tabla);

    document.Close();
    return results;
}

Solution

  • You use new Cell(a, b) like a and b were to be the row and column of the cell. But that's wrong, they are its rowspan and columnspan!

    That is not what you want. Thus, replace all your new Cell(a, b) calls by new Cell(1, 1) or new Cell().