Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

How To Create Style Heading1 in Office Open XML C#

$
0
0
 I try to create Heading1 style in the word document but nothing works.


I have this code to create style and document.

    public static void CreateWord(string filepath)
    {
        var wdoc = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document);
        var mdoc = wdoc.AddMainDocumentPart();

        AddStylesPartToPackage(wdoc);
        // Create the document structure and add some text.
        mdoc.Document = new Document();
        var body = mdoc.Document.AppendChild(new Body());

        CreateStyleHeading(mdoc,"Heading1","Heading 1");

        var par = body.AppendChild(new Paragraph());
        var run = par.AppendChild(new Run());
        var r = run.AppendChild(new Run());
        r.AppendChild(new Text("Create text in body - CreateWordprocessingDocument"));


        // ReSharper disable once UseMethodAny.2
        if (par.Elements<ParagraphProperties>().Count() == 0)
            par.PrependChild<ParagraphProperties>(new ParagraphProperties());
        // Get the ParagraphProperties element of the paragraph.
        var pPr = par.Elements<ParagraphProperties>().First();
        // Set the value of ParagraphStyleId to "Heading3".
        pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "Heading1" };

        wdoc.Close();

    }

    // Add a StylesDefinitionsPart to the document.  Returns a reference to it.
    public static StyleDefinitionsPart AddStylesPartToPackage(WordprocessingDocument doc)
    {
        StyleDefinitionsPart part;
        part = doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
        Styles root = new Styles();
        root.Save(part);
        return part;
    }

    public static void CreateStyleHeading(MainDocumentPart mdoc,  string styleid,string stylename)
    {



        var styles = mdoc.StyleDefinitionsPart.Styles;
        if (styles == null)
        {
            mdoc.StyleDefinitionsPart.Styles = new Styles();
            mdoc.StyleDefinitionsPart.Styles.Save();
        }

        // Create a new paragraph style element and specify some of the attributes.
        var style = new Style()
        {
            Type = StyleValues.Paragraph,
            StyleId = styleid,
            CustomStyle = true,
            Default = false
        };

        // Create and add the child elements (properties of the style).
        var uiPriority1 = new UIPriority { Val = 9 };

        //Aliases aliases1 = new Aliases() { Val = "" };
        var autoredefine1 = new AutoRedefine() { Val = OnOffOnlyValues.Off };
        var basedon1 = new BasedOn() { Val = "Normal" };
        var linkedStyle1 = new LinkedStyle() { Val = "OverdueAmountChar" };
        var locked1 = new Locked() { Val = OnOffOnlyValues.Off };
        var primarystyle1 = new PrimaryStyle() { Val = OnOffOnlyValues.On };
        var stylehidden1 = new StyleHidden() { Val = OnOffOnlyValues.Off };
        var semihidden1 = new SemiHidden() { Val = OnOffOnlyValues.Off };
        var styleName1 = new StyleName() { Val = stylename };
        var nextParagraphStyle1 = new NextParagraphStyle() { Val = "Normal" };
        var uipriority1 = new UIPriority() { Val = 1 };
        var unhidewhenused1 = new UnhideWhenUsed() { Val = OnOffOnlyValues.On };

        style.Append(uiPriority1);
        style.Append(autoredefine1);
        style.Append(basedon1);
        style.Append(linkedStyle1);
        style.Append(locked1);
        style.Append(primarystyle1);
        style.Append(stylehidden1);
        style.Append(semihidden1);
        style.Append(styleName1);
        style.Append(nextParagraphStyle1);
        style.Append(uipriority1);
        style.Append(unhidewhenused1);

        // Create the StyleRunProperties object and specify some of the run properties.
        var styleRunProperties1 = new StyleRunProperties();
        var bold1 = new Bold();
        var color1 = new Color() { ThemeColor = ThemeColorValues.Accent2 };
        var font1 = new RunFonts() { Ascii = "Lucida Console" };
        var italic1 = new Italic();
        // Specify a 12 point size.
        var fontSize1 = new FontSize() { Val = "24" };
        styleRunProperties1.Append(bold1);
        styleRunProperties1.Append(color1);
        styleRunProperties1.Append(font1);
        styleRunProperties1.Append(fontSize1);
        styleRunProperties1.Append(italic1);

        // Add the run properties to the style.
        style.Append(styleRunProperties1);

        // Add the style to the styles part.
        styles.Append(style);
    }





But when I run:

    CreateWord("file.docx");

When I open file.docx my style is renamed to "Heading 11" instead of "Heading 1" and there is no Navigation. ("This document does not contain headings"). [I cannot add pictures to this post]


I found only how to create style. But i cannot find information how to create Heading1 style. When i unpack created .docx and .docx created by a Word document i found only that Heading1 is added to "latentStyles" but I cannot find how to do this in the code.


Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>