sindhu jampani
sindhu jampani

Reputation: 524

Constant column for multiple rows

I was adding a table to pdf. I have 3 rows and 3 columns. I want the first column to appear only once as a single cell for all the rows. How can I do that?My code is as follows. My output should come like Deloitte in the column of company as shown in the image:

enter image description here

        PdfPTable table = new PdfPTable(dt.Columns.Count);
        PdfPRow row = null;
        float[] widths = new float[] { 4f, 4f, 4f };
        table.SetWidths(widths);
        table.WidthPercentage = 100;
        int iCols = 0;
        string colname = "";
        PdfPCell cells = new PdfPCell(new Phrase("Products"));
        cells.Colspan = dt.Columns.Count;

        foreach (DataColumn c in dt.Columns)
        {

            table.AddCell(new Phrase(c.ColumnName, fontbold));
        }

        foreach (DataRow r in dt.Rows)
        {
            if (dt.Rows.Count > 0)
            {
                table.AddCell(new Phrase(r[0].ToString(), font5));
                table.AddCell(new Phrase(r[1].ToString(), font5));
                table.AddCell(new Phrase(r[2].ToString(), font5));
            }
        } document.Add(table);

Upvotes: 0

Views: 1800

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

The MyFirstTable example from my book does exactly what you need. Ported to C#, it looks like this:

PdfPTable table = new PdfPTable(3);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
// we add the four remaining cells with addCell()
table.AddCell("row 1; cell 1");
table.AddCell("row 1; cell 2");
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");

You can look at the resulting PDF here. In your case you'd need

cell.Rowspan = 6;

For the cell with value Deloitte.

Upvotes: 1

Related Questions