views:

78

answers:

1

Good day, I'm trying to create a simple table with iTextSharp, so with c#. The goal is a table like this one:

http://i.imgur.com/US8N3.png

The problem is that if I apply the rowspan as 2 on cell A, iTextSharp does not render the rowspanned cell, this means that the cell have the same height of cell B. Here's the code:

    PdfPTable corporateTable = new PdfPTable(2);
    corporateTable.HeaderRows = 1;
    corporateTable.TotalWidth = pdfWidth - 50;

    PdfPCell vCell = new PdfPCell();
    vCell.Border = Rectangle.BOX;
    vCell.Rowspan = 2;
    vCell.Phrase = new Phrase("A", new Font(fontLh, 7f, 1, BaseColor.BLACK));
    corporateTable.CompleteRow();
    corporateTable.AddCell(vCell);


    PdfPCell vCellx = new PdfPCell();
    vCellx.Phrase = new Phrase("B", new Font(fontLh, 7f, 1, BaseColor.BLACK));
    vCellx.Colspan = 3;
    corporateTable.AddCell(vCellx);

    PdfPCell vCell1 = new PdfPCell();
    vCell1.Phrase = new Phrase("C", new Font(fontValue, 7f, 0, BaseColor.BLACK));
    corporateTable.AddCell(vCell1);

    corporateTable.WriteSelectedRows(0, -1, 100f, 100f, writer.DirectContent);
    document.Close();

What's wrong? I'm using the latest version of the dll. thanks and best regards

alberto

A: 

Well the basic answer is: it works! if you add two more cells, you will see that one cell (the one underneath A) is not filled.

But this is not what you expect (nor did I btw). To achive what you want use nested tables, that means:

  • create a table with two cols
  • insert cell A into table
  • create cell 2
  • create one more table with 1 col
    • insert cell B into table 2
    • insert cell c into table 2
  • insert table 2 into cell 2

search for itext rowspan, you will find multiple fully typed out examples.

hth

Mario

Mario The Spoon
Ok but this means that iTextSharp works differently from html, so I cannot do this without nesting the tables?
Alberto Sartori
This is what I found, yes. I played arround with your code as well, and the rowspan works, just the aligment is not justified as to be between the rows. I may check my iText in Action book somewhen tonite (MESZ). regards
Mario The Spoon
BTW: if you find the answer helpful, an upvote would be nice :-)
Mario The Spoon
Well I noticed that if I stroke the table using 'document.Add(corporateTable);' instead of 'corporateTable.WriteSelectedRows(0, -1, 0, -1, 300f, 100f, writer.DirectContent);' the rowspanned cell appears!
Alberto Sartori
is it now aligned between the two other rows? I used Add as well, and it was not aligned.
Mario The Spoon
Yes, if I use both "Add" and "writeselectedrows" there are 2 tables, one aligned on top and the other one is aligned as I put it (300,100). the problem concern also the size of table, the first one does not have the width I set :(
Alberto Sartori
tables are a tad beasty with iText (or pdf..). I will look up the examples in the book later on and post here.
Mario The Spoon
Mario The Spoon