This is a migrated thread and some comments may be shown as answers.

Programmatic keyword substitution in RichTextBox

2 Answers 96 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Aggie
Top achievements
Rank 1
Aggie asked on 12 Apr 2011, 01:08 AM
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:
foreach (Table table in radRichTextBox.Document.EnumerateChildrenOfType<Table>())
{
   foreach (Span span in table.EnumerateChildrenOfType<Span>())
   {
      if (span.Text == "[Heading]")
      {
         span.Text = ColumnDescriptions[0].DisplayName;
      }
   }
}
But how do I add more columns and set their titles? And how to add more rows? Please help, I'm really stumped!

2 Answers, 1 is accepted

Sort by
0
Shane Woodward
Top achievements
Rank 1
answered on 13 Apr 2011, 12:00 AM
Here is one way that I found and it seems to work (mostly and at least for now).

Once you've located the correct table you want to replace, you need to copy a header cell and the item cell for reference so you can apply the same styles to your new rows. Note that you need to do a deep copy of the cells otherwise you will lose all styling when you perform next step.

Then you delete all rows from your placeholder table and start adding new rows, whose cells are based on the copies of your template cells.

Some code:
Table headingTable = null;
TableCell headerCell = null;
TableCell itemCell = null;
 
foreach (Table table in radRichTextBox.Document.EnumerateChildrenOfType<Table>())
{
   foreach (TableCell cell in table.EnumerateChildrenOfType<TableCell>())
   {
      foreach (Span span in cell.EnumerateChildrenOfType<Span>())
      {
         if (span.Text == "[Heading]")
         {
            headingTable = table;
            headerCell = cell.CreateDeepCopy() as TableCell;
         }
         else if (span.Text == "[Items]")
         {
            itemCell = cell.CreateDeepCopy() as TableCell;
         }
      }
   }
}
 
// insert items
if (headingTable != null)
{
   // first delete the placeholder table rows
   headingTable.Rows.Clear();
                         
   // create the new rows using the same styles
   TableRow headings = CreateHeadings(headerCell);
   IEnumerable<TableRow> items = CreateItems(itemCell);
 
   // add new rows
   headingTable.AddRow(headings);
   foreach (TableRow row in items)
   {
      headingTable.AddRow(row);
   }
}

Hope this helps someone.
0
Alex
Telerik team
answered on 14 Apr 2011, 04:11 PM
Hello Aggie, Shane,

There is a demo which includes creating a document with code-behind table in this forum thread (CodeBehindDocument). If the table has been created, you can use the InsertTableColumnToTheLeft/Right and the analogical methods for rows. You can also review the other methods, which add content to the document dynamically (on button click).
I hope that helps.

Kind regards,
Alex
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Aggie
Top achievements
Rank 1
Answers by
Shane Woodward
Top achievements
Rank 1
Alex
Telerik team
Share this question
or