What I'm trying to achieve is mergefield functionality - keywords specified in templates to be replaced with data at runtime. This is relatively simple if I'm replacing a keyword with another static phrase but I want to also be able to create dynamic tables at runtime and insert them into the document.
The number of columns and rows is determined at runtime and the template contains a placeholder table with a header row and a single row, which is to be expanded to contain all the items.
For example, I have the following table in my template:
-------------------------------
| [Heading] |
-------------------------------
| [Items] |
-------------------------------
And I want to insert columns for Name, Description, Price and Quantity into the [Heading] placeholder.
Then I want to insert the following two items into the [Items] placeholder:
Item1: Name: Item1, Description: Description1, Price: 100, Quantity: 100
Item2: Name: Item2, Description: Description2, Price: 200, Quantity: 2
to get the following result:
--------------------------------------------------------------------
| Name | Description | Price | Quantity |
--------------------------------------------------------------------
| Item1 | Description1 | 100 | 100 |
--------------------------------------------------------------------
| Item2 | Description2 | 200 | 2 |
--------------------------------------------------------------------
I know I can find out if a table contains the placeholder [Heading] by using the following code:
But how do I add more columns and set their titles? And how to add more rows? Please help, I'm really stumped!
The number of columns and rows is determined at runtime and the template contains a placeholder table with a header row and a single row, which is to be expanded to contain all the items.
For example, I have the following table in my template:
-------------------------------
| [Heading] |
-------------------------------
| [Items] |
-------------------------------
And I want to insert columns for Name, Description, Price and Quantity into the [Heading] placeholder.
Then I want to insert the following two items into the [Items] placeholder:
Item1: Name: Item1, Description: Description1, Price: 100, Quantity: 100
Item2: Name: Item2, Description: Description2, Price: 200, Quantity: 2
to get the following result:
--------------------------------------------------------------------
| Name | Description | Price | Quantity |
--------------------------------------------------------------------
| Item1 | Description1 | 100 | 100 |
--------------------------------------------------------------------
| Item2 | Description2 | 200 | 2 |
--------------------------------------------------------------------
I know I can find out if a table contains the placeholder [Heading] by using the following code:
foreach
(Table table
in
radRichTextBox.Document.EnumerateChildrenOfType<Table>())
{
foreach
(Span span
in
table.EnumerateChildrenOfType<Span>())
{
if
(span.Text ==
"[Heading]"
)
{
span.Text = ColumnDescriptions[0].DisplayName;
}
}
}