【问题标题】:Adding Custom Heading to Word Document将自定义标题添加到 Word 文档
【发布时间】:2013-05-15 13:53:11
【问题描述】:

我正在尝试使用 Office Open XML SDK 将自定义标题添加到 Word 文档。我想继承默认的 Heading1 样式,但由于某种原因,下面的代码从头开始创建了一个 styles.xml 文件,并且只包含我的新样式。

我希望生成的 styles.xml 也包含默认样式(Normal、Heading1、Heading2、Heading3...)。我该怎么办?

这是我的代码:

        using (var package = WordprocessingDocument.Create(tempPath, WordprocessingDocumentType.Document))
        {
            // Add a new main document part. 
            var mainPart = package.AddMainDocumentPart();

            var stylePart = mainPart.AddNewPart<StyleDefinitionsPart>();

            // we have to set the properties
            var runProperties = new RunProperties();
            runProperties.Append(new Color { Val = "000000" }); // Color 
            runProperties.Append(new RunFonts { Ascii = "Arial" }); // Font Family
            runProperties.Append(new Bold()); // it is Bold
            runProperties.Append(new FontSize { Val = "28" }); //font size (in 1/72 of an inch)

            //creation of a style
            var style = new Style
            {
                StyleId = "MyHeading1",
                Type = StyleValues.Paragraph,
                CustomStyle = true
            };
            style.Append(new StyleName { Val = "My Heading 1" }); //this is the name
            // our style based on Heading1 style
            style.Append(new BasedOn { Val = "Heading1" });
            // the next paragraph is Normal type
            style.Append(new NextParagraphStyle { Val = "Normal" });
            style.Append(runProperties);//we are adding properties previously defined

            // we have to add style that we have created to the StylePart
            stylePart.Styles = new Styles();
            stylePart.Styles.Append(style);
            stylePart.Styles.Save(); // we save the style part

            ...
        }

这里是生成的styles.xml文件:

<?xml version="1.0" encoding="utf-8"?>
  <w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:style w:type="paragraph" w:styleId="MyHeading1" w:customStyle="true">
    <w:name w:val="My Heading 1" />
    <w:basedOn w:val="Heading1" />
    <w:next w:val="Normal" />
    <w:rPr>
      <w:color w:val="000000" />
      <w:rFonts w:ascii="Arial" />
      <w:b />
      <w:sz w:val="28" />
    </w:rPr>
  </w:style>
</w:styles>

【问题讨论】:

    标签: openxml-sdk heading


    【解决方案1】:

    我相信这是因为您是从头开始创建新文档,而不是基于模板创建文档。我认为 default 样式来自您的 Normal.dotm 模板(in C:\Users\&lt;userid&gt;\AppData\Roaming\Microsoft\Templates),您需要以此为基础创建文档。我所做的是将模板复制到文档文件名并更改文档类型(来自 VB.NET 的未经测试的 C# 翻译):

    public WordprocessingDocument CreateDocumentFromTemplate(string templateFileName, string docFileName)
    {
        File.Delete(docFileName);
        File.Copy(templateFileName, docFileName);
        var doc = WordprocessingDocument.Open(docFileName, true);
        doc.ChangeDocumentType(WordprocessingDocumentType.Document);
        return doc;
    }
    

    您的代码将类似于:

    using (var doc = CreateDocumentFromTemplate(normalTemplatePath, tempPath))
    {
        var stylePart = doc.MainDocumentPart.StyleDefinitionsPart;
        ...
    }
    

    希望有帮助!

    【讨论】:

    • 这就是我最终要做的。我会将此标记为答案。抱歉回复晚了。
    猜你喜欢
    • 1970-01-01
    • 2020-01-22
    • 2012-03-13
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多