Rowspan or nested? Create a table itextsharp

ε祈祈猫儿з 提交于 2019-12-25 01:43:40

问题


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.


回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/3872060/rowspan-or-nested-create-a-table-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!