Mixed font styling on a single line

I am working with the trial version of the Aspose PDF component and am trying to apply different styling to the text within a TextFragment component.


So as an example:

Name: Someones Full Name

As shown is how I’d like the text to appear. The category “Name” would be in bold text while the actual persons name would be in regular font.

So far I haven’t been able to figure a way to do this except to put the “Name” text in a TextFragment using a bold font and then creating another TextFragment with separate styling. I then move the second TextFragment to align it to the left of the first one using its Position attribute.

While I’ve gotten this to work, it isn’t a good solution because since the content is being generated dynamically; any lines added above this can potentially offset the positioning and throw things off.

Is there another way to have mixed styling of text on a single line???

Please Help!


Hi there,


Thanks for your inquiry. You need to use different TextSegments with different formatting to accomplish the task. Please check following sample code, it will help you to accomplish the task.

Document doc = new
Document();

Aspose.Pdf.Page page = doc.Pages.Add();

TextFragment fragment = new TextFragment();

TextSegment ts1 = new TextSegment();

ts1.Text = "Name: ";

ts1.TextState.Font = FontRepository.FindFont("Arial");

ts1.TextState.FontStyle = FontStyles.Bold;

ts1.TextState.FontSize = 12;

//ts1.TextState.LineSpacing = 13;

fragment.Segments.Add(ts1);

TextSegment ts2 = new TextSegment();

ts2.Text = "Someones First Name ";

ts2.TextState.Font = FontRepository.FindFont("Arial");

ts2.TextState.FontSize = 12;

ts2.TextState.FontStyle = FontStyles.Italic;

//ts2.TextState.LineSpacing = 13;

fragment.Segments.Add(ts2);

TextSegment ts3 = new TextSegment();

ts3.Text = "Someones Last Name";

ts3.TextState.Font = FontRepository.FindFont("Arial");

ts3.TextState.FontSize = 12;

//ts3.TextState.LineSpacing = 13;

fragment.Segments.Add(ts3);

page.Paragraphs.Add(fragment);

doc.Save("output.pdf");


Please feel free to contact us for any further assistance.

Best Regards,

Thank you so much for the reply! That is exactly what I needed.

Hi Mark,


Thanks for the acknowledgement.

We are glad to hear that your requirement is accomplished. Please continue using our API’s and in the event of any further query, please feel free to contact.