0

我想改变一个单词的颜色。我在 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

            }
4

1 回答 1

0

当您只为句子的一部分着色时,Word 在幕后所做的是将句子分成多个运行。你需要做同样的事情。

于 2015-08-11T13:53:41.720 回答