I've got a few different tables that I need to insert into a document at certain places and not sure how to do this. I have some code to create a table similar to this:
Which is great, however it inserts the table at the end of the document and I need to place it somewhere in the middle. Can I set this up with a content control somehow? Does anyone have an example on how to this?
The example appends at the end of the Body. You would need to insert it before or after the desired paragraph instead. I don't know usually use that API, so I don't know the exact methods involved.
If you have content control Name/Tag, you can get the SdtBlock object. Suppose you want to insert the table after/before that then you can use any of the built in methods InsertAfterSelf<T>(...), InsertAfter<T>(...), InsertBeforeSelf<T>(...) and InsertBefore<T>(...).
block.InsertAfterSelf(table); // this will insert the table after the content block.
Thanks heaps guys,
I managed to get it to work using this code to insert the table after a specific content control:
foreach (SdtBlock block in doc.Body.Descendants<SdtBlock>()
.Where(e => e.Descendants<SdtAlias>().FirstOrDefault().Val == cc))