Search code examples
c#rowpdfsharpmigradoc

MigraDoc: How to extend the row height when adding text?


I am building a table in MigraDoc. I have found a way to put a Table into a Table.Row.Cell with the help of a TextFrame. Unfortunately the Row.Cell does not grow when adding new entries into the TextFrame. So at a certain point the inner table overlaps into the rows beneath.

Here is my Code:

this.Table = this.MigraDokument.AddSection().addTable();
Row row = this.Table.AddRow();
TextFrame Frame = row.Cells[0].AddTextFrame();

Table k_table = Frame.AddTable();
// adding rows with 
// Row row2 = k_table.AddRow();

How can I tell the Row.Cell to grow with every entry that I put into the inner Table?

Edit: My problem was not that I could not add a nested table, like in [MigraDoc - imbricated / nested tables?. Although the answer from the link helped me. This question deals with the topic that TextFrames might be an inappropriate way to nest tables in tables because the Cell does not scale with the nested table.


Solution

  • The undocumented feature from [MigraDoc - imbricated / nested tables? was the Answer: Now the code looks this way and it works fine.

    this.Table = this.MigraDokument.AddSection().addTable();
    Row row = this.Table.AddRow();
    // Here I grab the cell that I want to populate later
    Cell dataCell = row.Cells[0];
    
    // Than I build the table with alle the necessary Information in it
    Table k_table = new Table();
    // adding columns and rows with 
    k_table.AddColumn();
    k_table.AddColumn();
    Row row = k_table.AddRow();
    // and populate with data
    row.Cells[0].AddParagraph("Stuff 1");
    row.Cells[1].AddParagraph("Stuff 2");
    
    // The final trick is to add it in the end to the `Elements`
    // property of the cell
    dataCell.Elements.add(k_table);
    

    The last step has the effect that the Table-Cell has grown to the extend, that the nested table affords! No merging with extra rows beneath was necessary. This approach seems to be more flexible than using a TextFrame as I tried in my question.