Hello!
I can’t figure out how to implement printing in aspose word in one line of text designed in different Styles.
I.e. in MS Word I select the text and pick some different Styles in the pop-up menu for several parts of text in one line, not in a separate Paragraph.
Example of what I need is attached. Different styles in on line.docx (12.2 KB)
How can I do this?
@ramazanovam910 In your case character styles are applied to the runs in the paragraph:
<w:r w:rsidRPr="001A2DC5">
<w:rPr>
<w:rStyle w:val="Heading1Char"/>
<w:u w:val="single"/>
</w:rPr>
<w:t>This is text in Heading 1 style.</w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"> </w:t>
</w:r>
<w:r w:rsidRPr="001A2DC5">
<w:rPr>
<w:rStyle w:val="Heading2Char"/>
<w:u w:val="single"/>
</w:rPr>
<w:t>This is text in Heading 2 style.</w:t>
</w:r>
The achieve the same using Aspose.Words you should use character style type (see StyleType enumeration). For example see the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create character styles (you can create such styles in MS Word UI too)
Style charStyle1 = doc.Styles.Add(StyleType.Character, "charStyle1");
Style charStyle2 = doc.Styles.Add(StyleType.Character, "charStyle2");
charStyle2.Font.Bold = true;
builder.Font.Style = charStyle2;
builder.Write("Hello World: ");
builder.Font.Style = charStyle1;
builder.Write("Normal");
doc.Save(@"C:\Temp\out.docx");
You can check style type in MS Word UI:
If you need to have several paragraph styles applied in the same line, you can use style separator, which is a special paragraph break that does not produce a line break:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Write("Hello World: ");
// Insert style separator to use different paragraph styles in the same line.
builder.InsertStyleSeparator();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Write("Normal");
doc.Save(@"C:\Temp\out.docx");