0
votes

I am using iTextSharp version 5.4.5.0.

I am trying to print PdfPTable with Multiple PdfPCell. The Number of PdfPCell will be dynamic. So How can I assign width to the dynamically generated PdfPCell ?

I know how to assign width to Static and Fixed number of Cell. But for Dynamic cell, how can I assign width to each of the dynamically generated Cells ? The Number of PdfPCell is not fixed.

Please help me ?

Thanks.

1
By dynamic number of cells do you mean that some cells of a row may be combined as a colspan? Or do you mean the individual rows of your table structurally have nothing in common? - mkl
I mean, the number of column is not fixed in advance. The Cell will be generated dynamically based on data from database.If I have 5 records from db then 5 column and 15 records then 15 columns(PdfpCells). So how can I assign width to each of the Cell dynamically. For each row the, cell will be same. But the overall number of cell is not fixed in advance. I hope you understand now. Thanks - Herin
But as soon as you have created the cells for the first row, you do know, don't you? Thus, you can also add those cells to a List instead of a PdfPTable. As soon as the cells for the first row are finished, you know the number of columns of your table, can create a PdfPTable with exactly that number of columns, and add the cells from the list to the table. The next rows can then be added as usual. - mkl
Yes, I know the number of count for the columns, but how can I assign different width to different cell. I can create PdfPTable like : " PdfPTable table = new PdfPTable(noOfColumns); " But I can not write like this : "table.SetTotalWidth(new float[] { 7, 104, 35, 35, 35, 35, 35, 35 });" As the Cells are generated dynamically. I want to assign each cell as different width. - Herin
But the widths of the cells in each row are the same as the widths of the corresponding cells of each other row, aren't they? Thus, if you do as I proposed in my previous comment and create the PdfPTable after creating the cells of the first row, you then can set the cell widths. - mkl

1 Answers

2
votes

Even after some back and forth in comments to the original question, I am not entirely sure I understand the question correctly, but let's try:

So let us assume you do not know the number of columns beforehand but need to fetch the cells of the first row to get to know the number of columns and their widths. In that case you can simply do something like this:

public void CreatePdfWithDynamicTable()
{
    using (FileStream output = new FileStream(@"test-results\content\dynamicTable.pdf", FileMode.Create, FileAccess.Write))
    using (Document document = new Document(PageSize.A4))
    {
        PdfWriter writer = PdfWriter.GetInstance(document, output);
        document.Open();

        PdfPTable table = null;
        List<PdfPCell> cells = new List<PdfPCell>();
        List<float> widths = new List<float>();
        for (int row = 1; row < 10; row++)
        {
            // retrieve the cells of the next row and put them into the list "cells"
            ...
            // if this is the first row, determine the widths of these cells and put them into the list "widths"
            ...
            // Now create the table (if it is not yet created)
            if (table == null)
            {
                table = new PdfPTable(widths.Count);
                table.SetWidths(widths.ToArray());
            }
            // Fill the table row
            foreach (PdfPCell cell in cells)
                table.AddCell(cell);
            cells.Clear();
        }

        document.Add(table);
    }
}