简体   繁体   English

将样式应用于文档

[英]Applying Style To A Document

How can I apply a style to a document like "CSS", but programmatically with OpenXML? 如何使用OpenXML以编程方式将样式应用于“ CSS”之类的文档? I don't want to apply style to individual elements, but set defaults and have the document parts obey them. 我不想将样式应用于单个元素,但是要设置默认值并使文档部分服从它们。

I've found HTMLToOpenXML, but would prefer doing this myself. 我找到了HTMLToOpenXML,但希望自己做。 Any help would be appreciated. 任何帮助,将不胜感激。

Solved it, just use the OpenXML Productivity Tool, find the name of the part you looking. 解决了它,只需使用OpenXML Productivity Tool,找到您要查找的零件的名称。 Alternatively just open up the document and identify what you looking to change, like a Header or Part A or Normal. 或者,只需打开文档并确定要更改的内容,例如标题,A部分或普通。

Here's the code with sample changes: 这是带有示例更改的代码:

public static void GetAndSetStyleFromDoc(string file)
{
            bool styleExists = true;

            using (var document = WordprocessingDocument.Open(file,true))
            {

                // Get the Styles part for this document
                StyleDefinitionsPart part = document.MainDocumentPart.StyleDefinitionsPart;

                foreach (Style style in part.RootElement.Elements<Style>())
                {
                    // PartA can be changed for "Normal", "Header1" etc
                    if (style.StyleId.Value.Equals("PartA", StringComparison.InvariantCultureIgnoreCase))
                    {
                        style.StyleParagraphProperties.SpacingBetweenLines.Line = "276";
                        style.StyleRunProperties.FontSize.Val = "14";
                        style.StyleRunProperties.Color.Val = "4F81BD"; // font color

                        ParagraphBorders paragraphBorders1 = new ParagraphBorders();
                        TopBorder topBorder1 = new TopBorder(){ Val = BorderValues.Single, Color = "856363", Size = (UInt32Value)24U, Space = (UInt32Value)0U };
                        LeftBorder leftBorder1 = new LeftBorder(){ Val = BorderValues.Single, Color = "856363", Size = (UInt32Value)24U, Space = (UInt32Value)0U };
                        BottomBorder bottomBorder1 = new BottomBorder(){ Val = BorderValues.Single, Color = "856363", Size = (UInt32Value)24U, Space = (UInt32Value)0U };
                        RightBorder rightBorder1 = new RightBorder(){ Val = BorderValues.Single, Color = "856363", Size = (UInt32Value)24U, Space = (UInt32Value)0U };

                        paragraphBorders1.Append(topBorder1);
                        paragraphBorders1.Append(leftBorder1);
                        paragraphBorders1.Append(bottomBorder1);
                        paragraphBorders1.Append(rightBorder1);

                        style.StyleParagraphProperties.ParagraphBorders = paragraphBorders1;
                    }
                }

            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM