Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font color in OpenXML word document (C#)

Tags:

c#

openxml

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" };
like image 851
Rein Avatar asked Oct 30 '12 22:10

Rein


2 Answers

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);
like image 163
Joseph Duty Avatar answered Sep 18 '22 15:09

Joseph Duty


Well, I kind of brute forced my way to the answer, but it works.

List<RunProperties> runProps = element.Descendants<RunProperties>().ToList();
foreach (RunProperties rp in runProps)
{
    rp.Color = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "0EBFE9" };
}

If anyone has a more elegant solution please add it and I'll upvote it.

like image 38
Rein Avatar answered Sep 18 '22 15:09

Rein