替换控件内容中的文本 [英] Replace text in control content
问题描述
我有一个单词模板,需要在一些精确的位置插入一些文本.
I have a word template that I need to change insering some text at some precise spots.
我已经完成了标头,但是我只使用了InnerXML,我可以尝试不使用此功能,这会更好.
I already done an header but I used only the InnerXML and I can try to not use this function it could be better.
我的代码:
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是我的数据对象.
cv is my objet with my data.
因此,实际上,这样做不会改变我最后一句话中包含"Resume"的部分. 另外,我将在此单词上添加一些列表,但我不知道该怎么做.我试图在Internet和与OpenXML相关的站点(包括Eric White的站点)上找到一些信息,但是找不到解决方案.
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 :因此,我终于通过@petedelis修复了第一部分:
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 ?
推荐答案
您说要在文档中找到第一个表,然后在该表的第二行中找到以下代码:
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();
这篇关于替换控件内容中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!