我想改变一个单词的颜色。我在 runproperties 中添加颜色,但不是更改单个单词的颜色,而是更改整行的颜色。看代码。
void AppendStyle(string document, string word, string col)
{
try
{
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(document, true)) //Open file from path
{
var body = wordDoc.MainDocumentPart.Document.Body;
var paras = body.Elements<Paragraph>();
DocumentFormat.OpenXml.Wordprocessing.Color color = new DocumentFormat.OpenXml.Wordprocessing.Color();
foreach (var para in paras)
{
foreach (var run in para.Elements<Run>())
{
foreach (var text in run.Elements<Text>())
{
if (text.Text.Contains(word))
{
color.Val = col;
run.AppendChild(color);
return;
}
}
}
}
wordDoc.Close(); // close the template file
}