How can I apply style from document template

Hello,
I’m using Aspose Word 17.11 version.
I’ve defined styles in Word Template. I’m loading document with template
My code is below:

                  var document = new Document(@"Word_Template.dotx");
                var documentTheme = document.Theme;
                document.AttachedTemplate = "Word_Template.dotx";
                document.AutomaticallyUpdateSyles = true;
                var documentBuilder = new DocumentBuilder(document);
.

In a template, I’ve many styles but say, I’m interested in below styles
“Heading4,Content Heading 3 Sub-heading”,
“Case study text”

When I loop through document Styles, I’m getting defined styles.
(Style Name) - (Style Type)
“Heading 4” - “Paragraph”
“Heading 4 Char” - “Character”
“Case study text” - “Paragraph”

I’ve tried to apply say, heading 4 in below manners:
I’ve DocumentBuilder extension method has below:

  • With Style Identifier and Style Name
 public static void WriteByStyleName(this DocumentBuilder documentBuilder, string text, string styleName, StyleIdentifier styleIdentifier = StyleIdentifier.BodyText){
                documentBuilder.ParagraphFormat.StyleIdentifier = styleIdentifier
                documentBuilder.ParagraphFormat.Style.Name = styleName;
                documentBuilder.Writeln(text);
                documentBuilder.ParagraphFormat.Style.Name = "Body Text";
               documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText
}

I apply in this manner:

public static void WritelnHeading4(this DocumentBuilder documentBuilder, string text)
{
 documentBuilder.WriteByStyleName(text, "Heading 4", StyleIdentifier.Heading4, true);
}
documentBuilder.WritelnHeading4("My Heading 4 Text")
and so on..

When I inspect word document, the problem is
with heading 4, paragraph’s style is Heading 4, Content Heading 3 Sub-heading, but it is not bold and
with “Case study text”, paragraph has style but font color is not applied.

  • With assigning Style object
public static void WriteByStyleName(this DocumentBuilder documentBuilder, string text, string styleName, StyleIdentifier styleIdentifier = StyleIdentifier.BodyText)
        {

            Style style = documentBuilder.Document.Styles[styleName];
            if (style.Type == StyleType.Paragraph)
            {
                documentBuilder.CurrentParagraph.ParagraphFormat.Style = style;
                documentBuilder.Writeln(text);
                documentBuilder.CurrentParagraph.ParagraphFormat.Style =                     
                                                       documentBuilder.Document.Styles["Body Text"];
             }
        }

When I inspect word document, the problem is
with heading 4,paragraph’s style is Heading 4, Content Heading 3 Sub-heading, but it is not bold and
with “Case study text”, paragraph’s style is “Normal”

How can I resolve this issue? I’ve heading isssues throughout the document. They seems like they applied but Font Styles(e.g. Bold) didn’t applied.

What is difference between documentBuilder.CurrentParagraph.ParagraphFormat and documentBuilder.ParagraphFormat?

Thanks.

@hdesai19

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word and template documents.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Both properties are used to represent current paragraph formatting properties.

Hello Tahir,
I appreciate for your reply.

But currently, I am lacking of time to create samples. I’ll provide it soon when time permits.

Meanwhile, can you please tell me, what is the standard way to apply User Style from template to document and then switch back to normal style?

Thanks.

@hdesai19

You are setting the style of paragraph in your code correctly. You can set the style of paragraph as shown below.

DocumentBuilder documentBuilder = new DocumentBuilder();
documentBuilder.ParagraphFormat.ClearFormatting();
documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.User;
documentBuilder.Writeln(text);

documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
documentBuilder.Writeln(text);

We suggest you please read the following article.
Inserting Document Elements

Hello Tahir,

Below line is throwing an exception :

documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.User;

System.ArgumentException
HResult=0x80070057
Message=Cannot return user defined styles by style identifier.
Source=Aspose.Words
StackTrace:
at Aspose.Words.StyleCollection.(StyleIdentifier , Boolean )
at Aspose.Words.StyleCollection.(StyleIdentifier )
at Aspose.Words.ParagraphFormat.set_StyleIdentifier(StyleIdentifier value)

@hdesai19

You can set the user defined style as shown below.

documentBuilder.ParagraphFormat.StyleName = "User defined style";