Search code examples
c#pdfitextitext7

IText7 Column width issue


I have been trying to create a PDF document with iText7 and with different column sizes in the table, In my code, I have already set the width for each column. but I am unable to get the desired result.

version - itext7-core:7.1.15

Here is the partial code:

private void ManipulatePdf(string pdfPath)
{
    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfPath));
    Document doc = new Document(pdfDoc);
    doc.SetMargins(20, 20, 20, 20);
    Table table2 = new Table(UnitValue.CreatePercentArray(new float[] {10,20,40,10,10,20,10 })).UseAllAvailableWidth();

    
    table2.SetWidth(UnitValue.CreatePercentValue(100));
    
    table2.SetFixedLayout();

    table2.AddCell(getCellm1(300));
    table2.AddCell(getCellm2(240));
    table2.AddCell(getCellm3(100));
    table2.AddCell(getCellm4(100));
    table2.AddCell(getCellm5(100));
    table2.AddCell(getCellm6(300));
    table2.AddCell(getCellm6(300));
    doc.Add(table2);
    doc.Close();
}

private Cell getCellm7(int cm)
{
    Cell cell = new Cell(1, 7);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm6(int cm)
{
    Cell cell = new Cell(1, 6);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm5(int cm)
{
    Cell cell = new Cell(1, 5);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm4(int cm)
{
    Cell cell = new Cell(1, 4);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm3(int cm)
{
    Cell cell = new Cell(1, 3);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm2(int cm)
{
    Cell cell = new Cell(1, 2);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm1(int cm)
{
    Cell cell = new Cell(1, 1);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

output of the above code

from the above code am getting varied column sizes, the result which I want is given below:

this is how I want columns to look like: 2

If someone has used iText to do such a thing, any advice would be appreciated. Let me know if you need any more information.


Solution

  • Your percent values (10,20,40,10,10,20,10) add up to 120, not 100. Call to .UseAllAvailableWidth() is equivalent to table2.SetWidth(UnitValue.CreatePercentValue(100)); - no need to do them both.

    You also pass numbers to Cell constructors (e.g. new Cell(1, 4)). Those numbers don't actually indicate the position of the cell and rather mean cell row span and column span. This is the real reason of the unexpected layout. You don't have to pass anything to the cell constructor unless you want the cell to occupy multiple rows or columns.

    Fixing the above mistakes gives the following result:

    result

    The code for reference:

    private void ManipulatePdf(string pdfPath) {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfPath));
        Document doc = new Document(pdfDoc);
        doc.SetMargins(20, 20, 20, 20);
        Table table2 = new Table(UnitValue.CreatePercentArray(new float[] {8, 15, 38, 8, 8, 15, 8}))
            .UseAllAvailableWidth();
    
        table2.SetFixedLayout();
    
        table2.AddCell(CreateCell("S.No"));
        table2.AddCell(CreateCell("Item Code"));
        table2.AddCell(CreateCell("Description"));
        table2.AddCell(CreateCell("Qty"));
        table2.AddCell(CreateCell("Unit"));
        table2.AddCell(CreateCell("Unit Price"));
        table2.AddCell(CreateCell("P.Unit"));
    
        doc.Add(table2);
        doc.Close();
    }
    
    private Cell CreateCell(String text) {
        Cell cell = new Cell();
        Paragraph p = new Paragraph(text).SetFontSize(8);
        p.SetTextAlignment(TextAlignment.CENTER);
        cell.Add(p);
        return cell;
    }