Applying styles

When I try to create a document with this code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Write("Heading 1");
builder.InsertBreak(BreakType.ParagraphBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText;
builder.Font.Size = 10;
builder.Font.Bold = false;
builder.Write("text 1 blah blah blah blah blah blah blah blah blah blah blah ");
builder.InsertBreak(BreakType.ParagraphBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Write("Heading 2");
builder.InsertBreak(BreakType.ParagraphBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText;
builder.Font.Size = 10;
builder.Font.Bold = false;
builder.Write("text 2 blah blah blah blah blah blah blah blah blah blah blah ");
builder.InsertBreak(BreakType.ParagraphBreak);

doc.Save("test.docx");

In the rendered document, the second Heading is not formatted correctly - when i click on the text, it ‘says’ it is Heading 3 format but sure hasn’t been rendered that way! driving me nuts!

Is there something I am missing?

Hi there,

Thanks for your inquiry.

Please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting 1) to Run nodes by using Character Styles e.g. a Glyph Style, 2) to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles) and 3) you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

In your case, I suggest you please use the ParagraphFormat.ClearFormatting and Font.ClearFormatting as shown below. ParagraphFormat.ClearFormatting Resets to default paragraph formatting. Font.ClearFormatting resets to default font formatting.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Write("Heading 1");
builder.InsertBreak(BreakType.ParagraphBreak);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText;
builder.Font.Size = 10;
builder.Font.Bold = false;
builder.Write("text 1 blah blah blah blah blah blah blah blah blah blah blah ");
builder.InsertBreak(BreakType.ParagraphBreak);
builder.ParagraphFormat.ClearFormatting();
builder.Font.ClearFormatting();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Write("Heading 2");
builder.InsertBreak(BreakType.ParagraphBreak);
builder.ParagraphFormat.ClearFormatting();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText;
builder.Font.Size = 10;
builder.Font.Bold = false;
builder.Write("text 2 blah blah blah blah blah blah blah blah blah blah blah ");
builder.InsertBreak(BreakType.ParagraphBreak);
doc.Save(MyDir + "Out.docx");

That’s great, thanks. Much better than the workaround I was using!

Hi there,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.