I have a document template which I want to dynamically populate using C#. The template contains a repeating section that has a few text boxes and some static text. I want to be able to populate the text boxes and to add new section items when needed.
The code that almost works is as follows:
WordprocessingDocument doc = WordprocessingDocument.Open(@"C:\in\test.docx", true);
var mainDoc = doc.MainDocumentPart.Document.Body
.GetFirstChild<DocumentFormat.OpenXml.Wordprocessing.SdtBlock>()
.GetFirstChild<DocumentFormat.OpenXml.Wordprocessing.SdtContentBlock>();
var person = mainDoc.ChildElements[mainDoc.ChildElements.Count-1];
person.InsertAfterSelf<DocumentFormat.OpenXml.Wordprocessing.SdtBlock>(
(DocumentFormat.OpenXml.Wordprocessing.SdtBlock) person.Clone());
This however, produces a corrupted file because the unique IDs are also duplicated by the Clone method.
Any idea on how to achieve my goal?