I have a word template that I need to change insering some text at some precise spots.
I already done an header but I used only the InnerXML and I can try to not use this function it could be better.
My code:
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(destinationFile, true))
{
var mainDocumentPart = myDoc.MainDocumentPart;
//Body
var body = mainDocumentPart.Document.Body;
List<SdtElement> SdtBlocks = myDoc.MainDocumentPart.Document.Descendants<SdtElement>().ToList();
//var text = SdtBlocks[0].Descendants<Text>().First().Text;
//SdtBlocks[0].Descendants<Text>().First().Text.Replace(SdtBlocks[0].InnerText, cv.Resume);
foreach (var element in SdtBlocks)
{
if (element.InnerText.Contains("Resume"))
{
element.Descendants<Text>().First().Text = cv.Resume;
System.Diagnostics.Debug.WriteLine(element.Descendants<Text>().First().Text);
}
//foreach(var text in element.Descendants<Text>())
//{
//}
}
cv is my objet with my data.
So actually, doing this doesn't change the part containing "Resume" on my final word. Also, I will have some list to add on this word and I don't know how to. I tried to find some info on the internet and on openXML related site (including the one of Eric White) but I couldn't find the solution.
Any idea and to fix this and also for the second part ?
EDIT : So I finally fixed the 1st part thanks to @petedelis :
var text = SdtBlocks[0].Descendants<Text>().First().Text;
Paragraph newParagraph = new Paragraph();
Run newRun = new Run();
Text newText = new Text(cv.Resume);
newRun.Append(newText);
newParagraph.Append(newRun);
SdtBlocks[0].Parent.InsertBefore(newParagraph, SdtBlocks[0]);
SdtBlocks[0].Remove();
Now I am on the table part : My table looks like this :
I need to duplicate the second row for each mission. Actually I have this :
foreach (Mission mission in listeMission)
{
SdtRow newRow = new SdtRow();
SdtContentRow newContent = new SdtContentRow();
newParagraph = new Paragraph();
newRun = new Run();
Text cell = new Text(mission.Titre);
newRun.Append(cell);
newParagraph.Append(newRun);
newContent.Append(newParagraph);
newRow.Append(newContent);
rowTemplate.RemoveAllChildren();
rowTemplate.Append(newContent);
rowTemplate.Parent.InsertBeforeSelf(newRow);
}
But the result is not what I want. Any ideas ?