MigraDoc - imbricated / nested tables?
Asked Answered
U

1

7

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
Usufruct answered 30/3, 2016 at 8:39 Comment(0)
L
23

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.

Languishing answered 30/3, 2016 at 9:1 Comment(4)
It works fine. Works with TextFrame too. Thanks for that and the link to the Invoice sample !Usufruct
+1 for the 'Elements' trick. Would it be nice to have an .AddTable() shorthand in Cells to prevent confusionNadenenader
What do you mean its undocumented? There is not much or enough documentation on MigraDocCode or PDFSharp. There are examples, but not full explanations of all functions and objects that can be used. Merging rows and columns at least for the Web does not give as much control or is not as flexible. When you have a table that is dynamic according to data at run time it is tricky to try to build a MigraDocCore table cells that will accommodate this through merging row columns vertically.Breakwater
@JohnFoll There is no "AddTable" for table cells. "Undocumented" means this is not described as a feature and may no longer work after a major revamp. We used to create documentation from the source code (see sourceforge.net/projects/pdfsharp/files/pdfsharp/…), but didn't update this recently. There used to be an online version on NuDoq which is no longer available. The tools come and go so quickly. We recommend working with the PDFsharp source code, so you have the source code documentation in IntelliSense and can jump into the source with F12 to get further info.Languishing

© 2022 - 2024 — McMap. All rights reserved.