We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Document.Styles - StyleCollection doesn't contain unopened default Word styles

Hi,

I want to get a new Document with a dotx Word template (my file attached), but the StyleCollection doesn’t contain all of the word styles - e.g. it shows Heading 2, but doesn’t have Heading 1 or Heading 3. If you open the template, open some of these styles (e.g. Heading 1) and just press ok, then it will be included in the Document.Styles although there were no changes.

I can replicate this with the code below. If run before changes to the template, it will show:

Normal
Heading 2
Default Paragraph Font
Table Normal
No List
List Paragraph
Header
Header Char
Footer
Footer Char
Heading 2 Char

If run after opening Heading 1, Heading 3 and Heading 4 styles (just open and press ok), it will show:

Normal
Heading 1
Heading 2
Heading 3
Heading 4
Default Paragraph Font
Table Normal
No List
List Paragraph
Header
Header Char
Footer
Footer Char
Heading 2 Char
Heading 1 Char
Heading 3 Char
Heading 4 Char

The code to replicate the issue:

        Document doc = new Document(@"TemplateForAspose.dotx");

        StyleCollection styles = doc.Styles;

        for (int i = 0; i < styles.Count; i++)
        {
            Console.WriteLine(styles[i].Name);
        }

Could you please advise how I can get the access to the all Word Styles from my template but without opening them manually in Word?

Thanks
DominikaTemplateForAspose.docx (16.2 KB)

@acturisaspose Please accept our apologies for the belated response. In fact, the attached document does not contain within it the definitions of Heading 1, Heading 3 and other styles you specified. These are the so-called built-in styles, you can easily create them with Aspose.Words code just like MS Word does, and save them later in the output document.

Document doc = new Document("TemplateForAspose.dotx");

Style heading1 = doc.Styles[StyleIdentifier.Heading1];
Style heading2 = doc.Styles[StyleIdentifier.Heading2];
Style heading3 = doc.Styles[StyleIdentifier.Heading3];
Style heading4 = doc.Styles[StyleIdentifier.Heading4];

Assert.IsNotNull(heading1);
Assert.IsNotNull(heading2);
Assert.IsNotNull(heading3);
Assert.IsNotNull(heading4);

doc.Save("Output.docx");

Thank you for reply, it’s clear now

1 Like