views:

66

answers:

1

I've been searching about how to merge two cells and I've found two answers Rowspan and Nested. I can't make my table with those two functions because I don't know how to merge cells at the begining and at the end. I've been trying many ways but this is driving me crazy.

The table I want to make has 9 columns and 3 rows. So, I want the table looks like:

| Header 1 | Header 2 |       Header 3         | Header 4 | Header 5 |  
|          |          | H1 | H2 | H3 | H4 | H5 |          |          |  
|    D1    |    D2    | D3 | D4 | D5 | D6 | D7 |    D8    |    D9    |

Header 1: cs=1, rs=2.
Header 2: cs=1, rs=2.
Header 3: cs=5, rs=1.
      H1: cs=1, rs=1.
      H2: cs=1, rs=1.
      H3: cs=1, rs=1.
      H4: cs=1, rs=1.
      H5: cs=1, rs=1.
Header 4: cs=1, rs=2.
Header 5: cs=1, rs=2.

cs:colspan, rs:rowspan.

Header 3 contains H1, H2, H3, H4, H5.

I think the solution is pretty easy but I can't find it. I hope you can understand Which the problem is because I can't upload images yet.

Thanks.

A: 

There are a couple ways to do what you want to do. Since your table structure isn't overly complex perhaps the easiest approach is to create your PdfPTable with nine columns and add your cells one at a time using each cell's Rowspan and Colspan properties as you create rows.

The code below will create a table laid out exactly as you described:

    private void CreatePdf() {
        using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create)) {

            Document doc = new Document(new iTextSharp.text.Rectangle(800f, 800f));
            PdfWriter.GetInstance(doc, fs);
            doc.Open();

            PdfPTable table = new PdfPTable(9);

            // Hdeaer row.
            table.AddCell(GetCell("Header 1", 1, 2));
            table.AddCell(GetCell("Header 2", 1, 2));
            table.AddCell(GetCell("Header 3", 5, 1));
            table.AddCell(GetCell("Header 4", 1, 2));
            table.AddCell(GetCell("Header 5", 1, 2));

            // Inner middle row.
            table.AddCell(GetCell("H1"));
            table.AddCell(GetCell("H2"));
            table.AddCell(GetCell("H3"));
            table.AddCell(GetCell("H4"));
            table.AddCell(GetCell("H5"));

            // Bottom row.
            table.AddCell(GetCell("D1"));
            table.AddCell(GetCell("D2"));
            table.AddCell(GetCell("D3"));
            table.AddCell(GetCell("D4"));
            table.AddCell(GetCell("D5"));
            table.AddCell(GetCell("D6"));
            table.AddCell(GetCell("D7"));
            table.AddCell(GetCell("D8"));
            table.AddCell(GetCell("D9"));

            doc.Add(table);
            doc.Close();
        }
    }
    private PdfPCell GetCell(string text) {
        return GetCell(text, 1, 1);
    }
    private PdfPCell GetCell(string text, int colSpan, int rowSpan ) {
        PdfPCell cell = new PdfPCell(new Phrase(text));
        cell.HorizontalAlignment = 1;
        cell.Rowspan = rowSpan;
        cell.Colspan = colSpan;

        return cell;
    }

As you can see I've wrapped the construction of PdfPCells in helper methods that allow you to set each cell's text content and Rowspan and Colspan properties.

If you haven't already done so, I suggest you check out this tutorial on iTextSharp tables:

iTextSharp - Introducing Tables

Jay Riggs