0

如何将样式应用于“CSS”之类的文档,但使用 OpenXML 以编程方式?我不想将样式应用于单个元素,而是设置默认值并让文档部分服从它们。

我找到了 HTMLToOpenXML,但我更喜欢自己做。任何帮助,将不胜感激。

4

1 回答 1

1

解决了,只需使用 OpenXML Productivity Tool,找到您要查找的部件的名称。或者,只需打开文档并确定您要更改的内容,例如标题或 A 部分或正常。

这是带有示例更改的代码:

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

            }
}
于 2013-02-19T09:07:55.860 回答