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

Free Support Forum - aspose.com

How to apply style to a run of text using C#

Dear Sir or Madam,

Please see the document attached. Could you please suggest how can I replicate it using Aspose Words? The document has 2 different styles applied in the one paragraph, while Aspose allows to assign style to the whole paragraph only.

Regards,
Alexpara.docx (11.9 KB)

@AlexanderNovickov In your document threre are two runs with different character styles set. You can set character style on different runs in the paragraph using Font.Style property.
Also there is a feature called style separator, when you visually see text as one paragraph, but content is represented by two paragraphs with special properties set. See DocumentBuilder.insertStyleSeparator method.

Hi Alexey,

Thank you for your reply. Style Separator looks like what I need. Is it possible to insert it directly into the document rather than use DocumentBuilder? Something like:
paragraph.InsertAfter(New StyleSeparator);
?
I added a sample project - I would expect “Cash on hand” and “text” to be of the different styles, but they have the same one actuConsoleApp1.zip (355.0 KB)
ally.
Regards,
Alex

@AlexanderNovickov No, there is no way to create create a style separator using DOM.
In your code you add newly created Run into the original paragraph, but it should be added to the paragraph created by db.InsertStyleSeparator() . See the modified code:

db.InsertStyleSeparator();
db.ParagraphFormat.StyleName = "Normal";
Run r = new Run(docOriginal, "text");
db.CurrentParagraph.AppendChild(r);

Also, in your originaly attached document there is no style separator. There are simply two Run nodes with different styles. You can recreate the same using Aspose.Words DOM using code like the following:

Document doc = new Document();

Style st1 = doc.Styles.Add(StyleType.Character, "st1");
Style st2 = doc.Styles.Add(StyleType.Character, "st2");
st2.Font.Bold = true;

Run run1 = new Run(doc, "This is the first run");
Run run2 = new Run(doc, "This is the second run");
run1.Font.Style = st1;
run2.Font.Style = st2;

doc.FirstSection.Body.FirstParagraph.AppendChild(run1);
doc.FirstSection.Body.FirstParagraph.AppendChild(run2);

doc.Save(@"C:\Temp\out.docx");