Change font color in OpenXML word document (C#)

后端 未结 4 1194
小鲜肉
小鲜肉 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:30

    Color val should be 8 digits. For example Color.Val="FFFF0000" display string in red.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-02-14 05:41

    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.

    0 讨论(0)
  • 2021-02-14 05:55

    I had the need for something very similar to the OP in the sense that I had a whole bunch of plain text and other controls in a word document template which I was populating at runtime. I created an extension method to set text and other formatting bits and bobs. Hopefully this will help anyone who comes here in need like I did:

        public static void ReplaceText(this SdtElement element, string replacementText, bool? isBold = null, bool? isItalic = null, System.Drawing.Color? color = null, VerticalPositionValues? textVerticalType = null, int? fontSizeComplexScript = null)
        {
    
            // First try to get content blocks from the element
            IEnumerable<SdtContentBlock> childContentBlocks = element.ChildElements.OfType<SdtContentBlock>();
    
            //Function to generate the new run properties
            RunProperties SetupNewRunProperties(RunProperties oldRunProps) { 
    
                var props = new RunProperties();
    
                if (fontSizeComplexScript.HasValue && fontSizeComplexScript.HasValue)
                    props.FontSizeComplexScript.Val = fontSizeComplexScript.ToString();
                else if (oldRunProps?.FontSizeComplexScript != null)
                    props.FontSizeComplexScript = (FontSizeComplexScript)oldRunProps.FontSizeComplexScript.CloneNode(true);
    
                if (isBold.HasValue) 
                    props.Bold.Val = OnOffValue.FromBoolean(isBold.Value);
                else if(oldRunProps?.Bold != null)
                    props.Bold = (Bold)oldRunProps.Bold.CloneNode(true);
    
                if (isItalic.HasValue)
                    props.Italic.Val = OnOffValue.FromBoolean(isItalic.Value);
                else if (oldRunProps?.Italic != null)
                    props.Italic = (Italic)oldRunProps.Italic.CloneNode(true);
    
                if (textVerticalType.HasValue)
                    props.VerticalTextAlignment.Val = textVerticalType.Value;
                else if (oldRunProps?.VerticalTextAlignment != null)
                    props.VerticalTextAlignment = (VerticalTextAlignment)oldRunProps.VerticalTextAlignment.CloneNode(true);
    
                if (color.HasValue)
                {
                    if (props.Color != null)
                        props.Color.Val = color.Value.ToHexString();
                    else
                        props.Color = new Color() { Val = color.Value.ToHexString() };
                }
                else if (oldRunProps?.Color != null)
                {
                    props.Color = (Color)oldRunProps?.Color.CloneNode(true);
                }
    
                return props;
            }
    
            if (childContentBlocks.Count() > 0)
            {
                SdtContentBlock contentBlock = childContentBlocks.First();
                Paragraph para = contentBlock.ChildElements.OfType<Paragraph>().First();
                if (para != null)
                {
                    Run run = para.GetFirstChild<Run>();
    
                    run.RunProperties = SetupNewRunProperties(run.RunProperties);
    
                    Text text = run.GetFirstChild<Text>();
                    text.Text = replacementText;
                }
            }
            else
            {
                // Instead, try to get content runs from the element
                IEnumerable<SdtContentRun> childContentRuns = element.ChildElements.OfType<SdtContentRun>();
    
                if (childContentRuns.Count() > 0)
                {
                    Run run = childContentRuns.First().GetFirstChild<Run>();
    
                    run.RunProperties = SetupNewRunProperties(run.RunProperties);
    
                    Text text = run.GetFirstChild<Text>();
                    text.Text = replacementText;
                }
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题