OpenXML中的Word文档更改字体颜色(C#) [英] Change font color in OpenXML word document (C#)

查看:2404
本文介绍了OpenXML中的Word文档更改字体颜色(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找了几个小时,我似乎无法找到这个坚实的答案。我有我需要与外部数据编辑的文本内容控件的现有文档。如果其中一个控件的数据不存在,那么我就需要更换一个适当的通知文本,更改字体颜色。



我有文本输入和所有的工作就好了,那将似乎没有做的工作是改变字体颜色的唯一部分。当前代码我也没有给我任何错误,并通过这种方法运行得很好,但是当我看到成品的文件仍然是纯黑色的文本。



我的色彩改变方法:(输入是所有内容控件的同一个标签的列表)

 公共无效SetBlueText(列表< ; SdtElement> sdtElement)
{
的foreach(在sdtElement SdtElement元素)
{
如果(元素= NULL)
{
runProperties runProperties =元素! .Descendants&所述; RunProperties方式>()FirstOrDefault();
runProperties.Color =新DocumentFormat.OpenXml.Wordprocessing.Color(){瓦尔=0EBFE9};
}
}
}



此外,简化了这两行下降到只有这个/有相同的效果。

  element.Descendants< RunProperties方式>()。FirstOrDefault()颜色= 
新DocumentFormat.OpenXml.Wordprocessing.Color(){瓦尔=0EBFE9};


解决方案

我遇到了类似的问题,发现了一些原因以便你追加对象到RunProperties对象实际上影响的格式更新是否不工作(我已经注意到的模式是,如果你追加之前,做你的格式的文本,该文本的格式不沾)。



例如。这部作品(文字变得大胆,坎布里亚标题,颜色设置为蓝色)

 运行formattedRun =新的run() ; 
RunProperties runPro =新RunProperties();
RunFonts runFont =新RunFonts(){Ascii码=坎布里亚(标题),HighAnsi =坎布里亚(标题)};
黑体加粗=新款Bold();
文本文本=新文本(检查);
彩色色=新的色彩(){瓦尔=365F91,themeColor相关= ThemeColorValues.Accent1,ThemeShade =BF};
runPro.Append(runFont);
runPro.Append(黑体);
runPro.Append(彩色);
runPro.Append(文本);
formattedRun.Append(runPro);



但是,这并不(文字变得坎布里亚标题和大胆,但色彩停留在标准黑色)

 运行formattedRun =新的运行(); 
RunProperties runPro =新RunProperties();
RunFonts runFont =新RunFonts(){Ascii码=坎布里亚(标题),HighAnsi =坎布里亚(标题)};
文本文本=新文本(检查);
黑体加粗=新款Bold();
彩色色=新的色彩(){瓦尔=365F91,themeColor相关= ThemeColorValues.Accent1,ThemeShade =BF};
runPro.Append(runFont);
runPro.Append(黑体);
runPro.Append(文本);
runPro.Append(彩色);
formattedRun.Append(runPro);


I've been searching for hours and I just can't seem to find a solid answer for this. I have an existing document with content controls that I need to edit the text in with external data. If the data for one of the controls is not present, then I need to replace the text with an appropriate notice and change the font color.

I have the text entry and all that working just fine, the only part that won't seem to do its job is changing the font color. The current code I have doesn't give me any errors and is running through this method just fine, but when I look at the finished document its still the plain black text.

My color changing method: (the input is a list of all content controls with the same tag)

public void SetBlueText(List<SdtElement> sdtElement)
{
    foreach (SdtElement element in sdtElement)
    {
        if (element != null)
        {
            RunProperties runProperties = element.Descendants<RunProperties>().FirstOrDefault();
            runProperties.Color = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "0EBFE9" };
        }
    }
}

Also, simplifying those two lines down to just this / has the same effect

element.Descendants<RunProperties>().FirstOrDefault().Color = 
                        new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "0EBFE9" };

解决方案

I have run into similar issues and discovered that for some reason the order that you append objects to the RunProperties object actually impacts whether or not the formatting update works (The pattern I have noticed is if you append the text prior to doing your formatting, the formatting for that text does not stick).

e.g. this works (the text becomes bold, Cambria Headings, and the color is set to blue)

Run formattedRun = new Run();
RunProperties runPro = new RunProperties();
RunFonts runFont = new RunFonts() { Ascii = "Cambria(Headings)", HighAnsi = "Cambria(Headings)" };
Bold bold = new Bold();
Text text = new Text("TESTING");
Color color = new Color() { Val = "365F91", ThemeColor = ThemeColorValues.Accent1, ThemeShade = "BF" };
runPro.Append(runFont);
runPro.Append(bold);
runPro.Append(color);
runPro.Append(text);
formattedRun.Append(runPro);

but this does not (The text becomes Cambria Headings and Bold, but the color stays the standard black)

Run formattedRun = new Run();
RunProperties runPro = new RunProperties();
RunFonts runFont = new RunFonts() { Ascii = "Cambria(Headings)", HighAnsi = "Cambria(Headings)" };
Text text = new Text("TESTING");
Bold bold = new Bold();
Color color = new Color() { Val = "365F91", ThemeColor = ThemeColorValues.Accent1, ThemeShade = "BF" };
runPro.Append(runFont);
runPro.Append(bold);
runPro.Append(text);
runPro.Append(color);
formattedRun.Append(runPro);

这篇关于OpenXML中的Word文档更改字体颜色(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆