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?
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.
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.
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.