MigraDoc - imbricated / nested tables?

2019-02-24 10:37发布

I would like to add a table inside another table (inside a specific cell). I can't find a way to add a Table object to a Cell object. Is that simply impossible?

Alternatively, I may merge some cells, but I can't find any sample in MigraDoc website with cells merging.

Here is my code :

Table parentTable = new Table();
parentTable.AddColumn(Unit.FromCentimeter(9));
Row parentRow = parentTable.AddRow();
Cell parentCell = parentRow.Cells[0];

Table currentTable = new Table();
currentTable.AddColumn(Unit.FromCentimeter(4));
Row currentRow = currentTable.AddRow();
currentRow.Cells[0].AddParagraph("blablabla");

parentCell.Add(currentTable); // this does not work

1条回答
仙女界的扛把子
2楼-- · 2019-02-24 11:39

The Invoice sample uses merging:
http://www.pdfsharp.net/wiki/Invoice-sample.ashx

The keywords are MergeRight and MergeDown. Use MergeRight=1 to get a cell that spans two columns.

I think merging is the best approach if it does not get too complicated.

You can add TextFrame to a Cell and add a Table to a TextFrame to achieve nested tables. However you will have to deal with the row height as the table cell will not grow automatically when the contents of the TextFrame grow.

There is a trick to add a Table to a Cell or Paragraph in a cell using the generic Add method. Code hack that adds a table to a table cell:

parentCell.Elements.Add(currentTable);

This is an undocumented feature. Merging is the recommended approach.

Cells do not break to the next page, so adding tables to cells will work for small nested tables only.

查看更多
登录 后发表回答