Named Styles

Hi there,

What is the difference between these?

  • DocumentBuilder.getBold()
  • DocumentBuilder.getFont().getBold()
  • DocumentBuilder.getParagraphFormat().getStyle().getFont().getBold()

I am trying to write code that will:
  1. Insert an empty paragraph
  2. Apply a Named Style
  3. Possibly change bold/italic/underline/super/sub/strike
  4. Write some text
  5. Reset style to before step 3

I have tried storing the bold/italic/underline/super/sub/strike states before step 3 then reapplying them at step 5, however this OVERRIDES the styling of the Named Style applied in Step 2.

Any help much appreciated.

Perhaps some clarification - consider the following:

  1. A Named Style called myBold which mirrors the “default”/“normal” style and applies Bold
  2. Some source text that looks like this:

    This is some text with both a named style and inline styles applied
I parse this text from our internal document format and get 5 objects:
  1. A Paragraph object with a “myStyle” property of “myBold” and “children” of:
    1. A Text object containing the string "This is some text with both a "
    2. A Text object containing the string “named style” and an “Italic” attribute
    3. A Text object containing the string " and "
    4. A Text object containing the string “inline” and an “Italic” attribute
    5. A Text object containing the string " styles applied."

When I am appending this paragraph to an Aspose.Words Document:

  • The Paragraph object calls DocumentBuilder.getParagraphFormat().setStyleName(myStyle)
  • The first Text object does nothing other than call DocumentBuilder.write() with its value string.
  • The second Text object, upon finding it has an “Italic” attribute:
    • boolean wasItalic = DocumentBuilder.getFont().getItalic();
    • DocumentBuilder.getFont().setItalic(true);
    • DocumentBuilder.write(value);
    • DocumentBuilder.getFont().setItalic(wasItalic);
  • The other three Text objects perform the same.

If I call DocumentBuilder.getParagraphFormat().setStyleName(myStyle)
between each Text object, it will reset any style set by the previous
Text object.

My problem is in “resetting” any styling changes applied by each Text object before the next. In the example above, if the Named Style “myStyle” was Italic (instead of bold), the end result would look like this:

This is some text with both a named style and inline styles applied.

Instead of:

This is some text with both a named style and inline styles applied.

This is because DocumentBuilder.getFont().getItalic() returns false, even though DocumentBuilder.getParagraphFormat().getStyle().getFont().getItalic() would return true. I assume this is because Named Styles and Inline Styles are different.


Thanks for your inquiry.

Please note that in MS Word, document formatting is applied on a few 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 Styles, 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.

DocumentBuilder.getBold() & DocumentBuilder.getFont().getBold(): Both of these properties are used to specify Bold attribute for the current Run of text. For example, if you set them to true and then use DocumentBuilder.write(“some text”) method, the text will appear as Bold in Document.

DocumentBuilder.getParagraphFormat().getStyle().getFont().getBold(): Suppose you have may sentences (Runs of text) in a single Paragraph; if you want the first and last sentences be displayed as Bold and all other Runs inside that Paragraph as Normal, then you should set the formatting on Run levels before writing first, second and last sentences. On the other hand, if yo want all the Runs in the current Paragraph be displayed as Bold, then you need to specify the formatting on the Paragraph level only once.

Moreover, I would suggest you please try using the following code snippet:

Document doc = new Document();

// 1 Insert an empty paragraph
Paragraph para = new Paragraph(doc);

// 2 Apply a Named Style
Style style1 = doc.getStyles().add(StyleType.PARAGRAPH, “MyStyle1”);

// Apply the paragraph style to the current paragraph in the document and add some text.

// Add a sentence of text
Run run = new Run(doc);
run.setText("Hello World! ");


// Add another sentence of text
Run run2 = new Run(doc);
run2.setText("No direct formatting specified. Formatting from parent Paragraph style will be inherited. ");


I hope, this will help

Best Regards,

Thank you very much for your response - I’m working on implementing our solution using this method now.

I’m not sure if I should start a new thread for such a small question so I’ll just ask here - how do I insert a Hyperlink into a Run?

I used to use something like

if (myTextObject.isHyperlink()) {
builder.insertHyperlink(myTextObject.getValue(), myTextObject.getURI(), false)
else {

Changing builder.write() to Run.setText() is trivial, but there is no Run.InsertHyperlink().

Again, thank you for your help.

Hi Alex

Thanks for your inquiry.

A Hyperlink in Ms Word document is represented by a Field element and I am afraid, you can’t insert a Field inside a Run node. However, you can insert Hyperlinks using DocumentBuilder.InsertHyperlink method inline with other Run nodes inside a Paragraph. I would suggest you please read the following API page:

If we can help you with anything else, please feel free to ask.

Best Regards,