开发者

Rowspan or nested? Create a table itextsharp

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.


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


Robert (below) is right! Rowspan is not supported in the latest versions of iTextSharp. Only colspan is supported. The URL provided above does state that it has been supported since v 2.1.6, but looks like that hasn't been updated in a while as there is no more setRowSpan() method in any of the new versions (above v5) and the RowSpan property setting is just ignored.

You're better off adding a empty cells for rowspanned cells.


Rowspan isn't a answer, because there is no rowspan in iTextSharp. You have to use nested tables.

Colspan is posible but not rowspan.

For your table, i would only use one colspan and use empty Cells between Header 1 and D1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜