Replace text in control content

岁酱吖の 提交于 2019-12-11 17:42:59

问题


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 ?


回答1:


You say you are finding the first table in the document, and then the second row of this table with this code :

// Find the first table in the document. 
Table table = myDoc.MainDocumentPart.Document.Body.Elements<Table>().Skip(‌​1).First(); 
// Find the second row in the table. 
SdtRow rowTemplate = table.Elements<SdtRow>().First();

I think you are in fact finding the second table in the document, and then the first row of this table with it.

Maybe try the following instead ?

// Find the first table in the document. 
Table table = myDoc.MainDocumentPart.Document.Body.Elements<Table>().First(); 
// Find the second row in the table. 
SdtRow rowTemplate = table.Elements<SdtRow>().Skip(1).First();


来源:https://stackoverflow.com/questions/44701541/replace-text-in-control-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!