Adding rows to a table based on a token

I am evaluating this library at the moment and the one thing I can’t see it, if I get a ContentRange, how do I get ahold of the element containing it?

So I want to create an empty table with a token. Then I want to find the token and insert rows based on a collection of data, finally deleting the row with the token, so I can use a token to find a table and build it with data in my doc gen.

Hi Christian,

Each ContentRange has a start and end ContentPosition and those positions have parent Element.
So, here is how you can get the location of your ContentRange:

DocumentModel document = DocumentModel.Load("input.docx");
ContentRange content = document.Content.Find("TOKEN").First();

Element start = content.Start.Parent;
Element end = content.End.Parent;
do
{
    if (start == end)
        Console.WriteLine(start.ElementType);
    else
        Console.WriteLine(start.ElementType + " - " + end.ElementType);

    start = start.Parent;
    end = end.Parent;
}
while (start != null);

For a table with the TOKEN text you would get this parents hierarchy:

Run
Paragraph
TableCell
TableRow
Table
Section
Document

Anyway, please take a look at Find and Replace examples, the second example shows how you can replace some placeholder text with a Table element.

However, instead of that have you considered using the mail merge feature?
Note, it can simplify this task for you, it can create a dynamic number of rows based on your collection of data. For instance, please check the Merge Ranges example.

I hope this helps.

Regards,
Mario