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

Free Support Forum - aspose.com

Is there a way to change the color of a particular character in TextFragment?

TextFragment fragment = new TextFragment(“Q. Thank you for reading my question”);

In the above string, I would like to change only Q. to red.

What should I do?


Thank you for contacting support.

We would like to share with you that one TextFragment can include many Text Segments so you can break a TextFragment into several TextSegments and manipulate them as per your requirements. Please try using below code snippet as an example to achieve your requirements.

        Document document = new Document();
        Page page = document.Pages[1];
        var textfragment1 = new TextFragment("Q. Thank you for reading my question");
        var textfragment2 = new TextFragment("");

        TextSegmentCollection collection1 = textfragment1.IsolateTextSegments(0, 2);
        TextSegmentCollection collection2 = textfragment1.IsolateTextSegments(2, textfragment1.Text.Length);
        TextSegment segment1 = new TextSegment();

        segment1 = collection1[1];
        segment1.TextState.ForegroundColor = Color.Red;

        TextSegment segment2 = new TextSegment();
        segment2 = collection2[1];


        document.Save(dataDir + "Test_18.5.pdf");

It adds different text in a single line. We hope this will be helpful. Please feel free to contact us if you need any further assistance.