iTextSharp - Use Colspan with PdfPRow
Asked Answered
C

2

7

I am able to create multiple rows if they contain the same number of columns:

table = new PdfPTable(3);

var firstRowCell1 = new PdfPCell( new Phrase ("Row 1 - Column 1"));
var firstRowCell2 = new PdfPCell( new Phrase ("Row 2 - Column 2"));
var firstRowCell3 = new PdfPCell( new Phrase ("Row 3 - Column 3"));
PdfPCell[] row1Cells = { firstRowCell1, firstLineRow2, firstRowCell3 };
var row1 = new PdfPRow(row1Cells);
table.Rows.Add(row1);

var nextRowCell1 = new PdfPCell( new Phrase ("Row 2 - Column 1"));
var nextRowCell2 = new PdfPCell( new Phrase ("Row 2 - Column 2"));
var nextRowCell3 = new PdfPCell( new Phrase ("Row 2 - Column 3"));
PdfPCell[] row2Cells = { nextRowCell1, nextRowCell2, nextRowCell3 };
var row2 = new PdfPRow(row2Cells);
table.Rows.Add(row2);

This works fine giving me two rows each with three columns.

enter image description here

However if I want the first row to just have one long column using Colspan it disappears:

var table = new PdfPTable(3);  

var firstRowCell1 = new PdfPCell(new Phrase("Row 1 - Column 1"));
firstRowCell1.Colspan = 3;
PdfPCell[] row1Cells = { firstRowCell1 };
var row1 = new PdfPRow(row1Cells);
deptHeaderTable.Rows.Add(row1);

var nextRowCell1 = new PdfPCell(new Phrase("Row 2 - Column 1"));
var nextRowCell2 = new PdfPCell(new Phrase("Row 2 - Column 2"));
var nextRowCell3 = new PdfPCell(new Phrase("Row 2 - Column 3"));
PdfPCell[] row2Cells = { nextRowCell1, nextRowCell2, nextRowCell3 };
var row2 = new PdfPRow(row2Cells);
deptHeaderTable.Rows.Add(row2);

enter image description here

There are no errors given it just simply does not render.

Additionally, I am aware of table.AddCell which automatically starts a new row when the table column limit is reached for the current row. However, I want to use PdfPRow if at all possible.

Any help would be greatly appreciated.

Crocked answered 8/11, 2013 at 16:32 Comment(0)
M
20

Seems like you've read documentation that isn't endorsed by the original iText developer (being me). May I ask you for the URL where you've found that documentation, so that I can ask for a cease and desist?

As for the answer to your question: please take a look at the official documentation, and you'll find the MyFirstTable example. Based on this example, you can adapt your own code as follows:

var table = new PdfPTable(3);
var cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
table.AddCell("row 2; cell 3");

As you can see, there's no reason to use the PdfPRow class. The PdfPRow class is used internally by iText, but as documented in my book, developers using iText shouldn't use that class in their code.

Manny answered 9/11, 2013 at 10:39 Comment(3)
Thank you for your quick response. I will switch my code over to using AddCell() and have it automatically start the next row when the colspan is reached. I'll put a comment between each "row" for readability. I was exploring the public methods of the iTextSharp.text.pdf; namespace and found PdfPRow along side PdfPTable and PdfPCell. Additionally, there are several examples of using it on this site: #4044348 and a few on some external sites: jexp.ru/index.php/Java_Tutorial/PDF/Table_Row Thanks again!Crocked
@Bruno Lowagie: No wonder that most examples have table.AddCell instead of table.Rows.Add(somePdfPRow object). I still think that it is more natural to create cells, add these cells to a row and then add the row to table.Rows. Using table.AddCell only makes it very hard to track down where I added an extra cell or where I missed one cell, because it'll blindly add cells to the table and start a new row when the number of defined columns has exhausted in a single row. Do you agree?Flores
Personally, I think it's easier to let iText handle all the row stuff, but that's a matter of opinion. There are pros and cons for both approaches.Manny
I
2

Though I would recommend using code as per the official documentation as recommended above by the original Author (Thank you sir !!!), there is a way to resolve the issue:

    public static void TestFunction()
    {
        string[] cells = new string[] { "C1", "C2", "C3", "C6" };
        float[] columnWidths = new float[] { 25f, 25f, 25f, 25f, 25f, 25f };
        int[] colSpans = new int[] { 1, 1, 3, 1 };

        PdfPTable pdfPTable = new PdfPTable(columnWidths);

        var row = CreatePdfRow(cells, colSpans);
        pdfPTable.Rows.Add(row);
    }

    public static PdfPRow CreatePdfRow(string[] cells, int[] colSpans = null)
    {
        // parameter values passed for cells and colSpans:
        // string[] cells = new string[]{ "C1", "C2", "C3", "C6"};
        // int[] colSpans = new int[] { 1,1,3,1};

        if (cells == null || cells.Length <= 0)
            return new PdfPRow(new PdfPCell[] { new PdfPCell(new Phrase("")) });

        List<PdfPCell> Cells = new List<PdfPCell>();

        for (var i = 0; i < cells.Length; i++)
        {
            var pdfPCell = new PdfPCell(new Phrase(cells[i] ?? ""));

            if (colSpans != null && colSpans.Length > i)
                pdfPCell.Colspan = colSpans[i];

            Cells.Add(pdfPCell);

            // For cells with ColSpan > 1, add null cells to fill up the empty spots
            if (colSpans != null && colSpans.Length > i && colSpans[i] > 1)
            {
                for (var j = 1; j < colSpans[i]; j++)
                {
                    Cells.Add(null);
                }
            }
        }

        PdfPRow row = new PdfPRow(Cells.ToArray());
        return row;
    }
Insinuating answered 22/5, 2018 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.