Dynamically format paragraph contents in Aspose word dot net

Hi Team,

We have a web application where we have a functionality that allows user to type in a content from front end and we will insert it as a paragraph in word document through Aspose word. Here user will be given options to format the content like to apply bold, italics, underline and change font size and color for certain text in a paragraph.

So when we apply this in word how to apply the formatting for content in the middle of a paragraph programmatically through Aspose word. Kindly share a sample code for this.

Thanks,
Karthikeyan

@Karthik_Test_account Run class is intended for the purposes specified by you.

Document doc = new Document();

Run run1 = new Run(doc, "Foo");
Run run2 = new Run(doc, " bar");
Run run3 = new Run(doc, " baz");
Run run4 = new Run(doc, " etc.");

run1.Font.Size = 21;
run2.Font.Bold = true;
run3.Font.Italic = true;
run4.Font.Bold = true;
run4.Font.Italic = true;
run4.Font.HighlightColor = Color.Yellow;

doc.FirstSection.Body.FirstParagraph.AppendChild(run1);
doc.FirstSection.Body.FirstParagraph.AppendChild(run2);
doc.FirstSection.Body.FirstParagraph.AppendChild(run3);
doc.FirstSection.Body.FirstParagraph.AppendChild(run4);
doc.Save("TestRun.docx");

You can read more details here.

Thanks @Vadim.Saltykov this helps !

I also have one more question. I know we can insert a new paragraph at any position in a word template but while inserting a para is it possible to mention if that para needs to have a bulletin and the type of bulletin as well. kindly let us know if this can be done

Thanks,
Karthikeyan

@Karthik_Test_account
Please clarify what you mean by “para needs to have a bulletin”. Could you please attach a Word document that has the content you expect.
If you simply need to make a paragraph a list item, you can use Paragraph.ListFormat property to apply list formatting. Please see our documentation to learn more about working with lists.

@vadim.saltykov - As a continuation to the above topic I have a situation where I have a predefined para in a word document as follows. Here I need to make style changes to a part of a para say the text “Nothing but the wind rustling the leaves could be heard” italics and bold. How to achieve this? can you please share a sample code for this?

Actual Para:
Was it a whisper or was it the wind? He wasn’t quite sure. He thought he heard a voice but at this moment all he could hear was the wind rustling the leaves of the trees all around him. He stopped and listened more intently to see if he could hear the voice again. Nothing but the wind rustling the leaves could be heard. He was about to continue his walk when he felt a hand on his shoulder, and he quickly turned to see who it was. There was nobody there, but he heard the voice again.

Final Para:
Was it a whisper or was it the wind? He wasn’t quite sure. He thought he heard a voice but at this moment all he could hear was the wind rustling the leaves of the trees all around him. He stopped and listened more intently to see if he could hear the voice again. Nothing but the wind rustling the leaves could be heard. He was about to continue his walk when he felt a hand on his shoulder, and he quickly turned to see who it was. There was nobody there, but he heard the voice again.

Thanks,
Karthikeyan

@Karthik_Test_account Ii think the easiest way to achieve what you need is using find/replace functionality. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// get paragraph.
Paragraph para = doc.FirstSection.Body.FirstParagraph;
// Make part of text bold and italic.
FindReplaceOptions options= new FindReplaceOptions();
options.ApplyFont.Bold = true;
options.ApplyFont.Italic = true;
options.UseSubstitutions= true;
para.Range.Replace(@"Nothing but the wind rustling the leaves could be heard", "$0", options);

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

@alexey.noskov - Also, how to set color with hexadecimal code. I know we can set normal color with string. Can you please share the code to set the color with hexadecimal?

Thanks,
Karthikeyan

@Karthik_Test_account Aspose.Words uses System.Drawing.Color struct to return or set color of elements in the document. So please see System.Drawing.Color struct to learn how to create it.