Change font color in OpenXML word document (C#)

后端 未结 4 1195
小鲜肉
小鲜肉 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: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 childContentBlocks = element.ChildElements.OfType();
    
            //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().First();
                if (para != null)
                {
                    Run run = para.GetFirstChild();
    
                    run.RunProperties = SetupNewRunProperties(run.RunProperties);
    
                    Text text = run.GetFirstChild();
                    text.Text = replacementText;
                }
            }
            else
            {
                // Instead, try to get content runs from the element
                IEnumerable childContentRuns = element.ChildElements.OfType();
    
                if (childContentRuns.Count() > 0)
                {
                    Run run = childContentRuns.First().GetFirstChild();
    
                    run.RunProperties = SetupNewRunProperties(run.RunProperties);
    
                    Text text = run.GetFirstChild();
                    text.Text = replacementText;
                }
            }
    
        }
    

提交回复
热议问题