Change font color in OpenXML word document (C#)

后端 未结 4 1208
小鲜肉
小鲜肉 2021-02-14 04:58

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 ex

4条回答
  •  清酒与你
    2021-02-14 05:31

    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);
    

提交回复
热议问题