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

Free Support Forum - aspose.com

Insert Style Separator to Put Different Paragraph Styles

I would like to insert Style Separator to put different paragraph styles in one paragraph. In the MsWord I can do it by Ctrl+Alt+Enter (see http://www.addbalance.com/usersguide/styles.htm#Style_Separator). But how to do this by Aspose.Words Java API?

Thank you.

@dyuzhev

Thanks for your inquiry. Please note MS Word has four types of styles - character, paragraph, list and table. You can achieve your requirements by using Paragraph Styles and Character styles as following. It will insert text with different styles in a Paragraph. Hopefully it will help you to accomplish the task.

However, if there is any difference in your requirements and my understanding then please share some more details along with expected document as ZIP file. We will further investigate and will guide you accordingly.

com.aspose.words.Document document = new com.aspose.words.Document();

//Create a character custom Character style
Style charstyle = document.getStyles().add(StyleType.CHARACTER, "MyCharStyle");
charstyle.getFont().setBold(false);
charstyle.getFont().setSize(8);
charstyle.getFont().setName("Arial");

DocumentBuilder builder = new DocumentBuilder(document);
//apply Paragraph style - All Heading styles are Paragaph styles.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);

//Insert some text
builder.write("Heading 1");

//Now we can apply Character style
builder.getFont().setStyleName("MyCharStyle");
builder.write("This is text with some other formatting ");

document.save("Diff_Text_Style.docx");

@tilal.ahmad
Thank you for your reply. But I have some different requirements. I need to insert the Table Of Content with text “Heading 1” only, i.e. without the text “This is text with some other formatting”, but all text should be placed in one paragraph. Please see attached example Diff_Text_Style_By_Word.zip (13.1 KB) (Please change the extension from .zip to .docx.)

I’ve tried to insert ToC with some modification of your code:

    DocumentBuilder builder = new DocumentBuilder(document);
    // Insert a table of contents at the beginning of the document.
    builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
    ...
    document.updateFields();
    document.save("Diff_Text_Style.docx");

but I get the whole text “Heading 1This is text with some other formatting” in ToC.

Thank you again.

@dyuzhev

Thanks for sharing additional information. After initial investigation, we have logged a new feature request for your requirements WORDSNET-15943 in our issue tracking system. We will notify you as soon as it is resolved.

We are sorry for the inconvenience.

@dyuzhev,

The issues you have found earlier (filed as WORDSNET-15943) have been fixed in this Aspose.Words for .NET 18.2 update and this Aspose.Words for Java 18.2 update.

Please also check the following articles:

@dyuzhev

Starting from Aspose.Words 18.2, you can insert style separator into the document. The DocumentBuilder.InsertStyleSeparator method allows to apply different paragraph styles to two different parts of a text line.

Please read more detail and code example from the following article.
Inserting Style Separator to Put Different Paragraph Styles