Question

I am trying to add custom headings to a Word Document using the Office Open XML SDK. I want to inherit the default Heading1 style but for some reason the code below creates a styles.xml file from scratch and only contains my new styles.

I want the generated styles.xml to also contain the default styles (Normal, Heading1, Heading2, Heading3,...). What should I do?

Here is my code:

        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

            ...
        }

Here is the generated styles.xml file:

<?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>
Was it helpful?

Solution

I believe that this is because you are creating a new document from scratch, and not basing your document on a template. I think the default styles come from your Normal.dotm template (in C:\Users\<userid>\AppData\Roaming\Microsoft\Templates), and you need to base your document on this. What I have done is copy the template to the document file name and change the document type (untested C# translation from VB.NET):

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

Your code would then be something like:

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

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top