Add text in PDF with different styles like bold and normal using Aspose.PDF for .NET

I’ve tried to concatenate TextFragment like this:

TextFragment secondParagraphBoldPartOne = new TextFragment("Lorem ipsum dolor sit amet, ");
secondParagraphBoldPartOne.TextState.FontStyle = FontStyles.Bold;
TextFragment secondParagraphBoldPartTwo = new TextFragment(“adipiscing elit.Mauris sem felis, blandit et viverra vel, egestas nec lacus. Quisque at.”);
secondParagraphBoldPartTwo.TextState.FontStyle = FontStyles.Bold;
TextFragment secondParagraph = new TextFragment( secondParagraphBoldPartOne+ "consectetur " + secondParagraphBoldPartTwo);

page.Paragraphs.Add(secondParagraph);

But he returns this
“Aspose.Pdf.Text.TextFragmentconsectetur Aspose.Pdf.Text.TextFragment”, and I don’t know how to mix them.

@biomath421

Please try using the following code snippet in order to keep all text fragments inline by specifying the respective property:

TextFragment secondParagraphBoldPartOne = new TextFragment("Lorem ipsum dolor sit amet, ");
secondParagraphBoldPartOne.TextState.FontStyle = FontStyles.Bold;
secondParagraphBoldPartOne.IsInLineParagraph = true;
TextFragment secondParagraphBoldPartTwo = new TextFragment("adipiscing elit.Mauris sem felis, blandit et viverra vel, egestas nec lacus.Quisque at.");
secondParagraphBoldPartTwo.TextState.FontStyle = FontStyles.Italic;
secondParagraphBoldPartTwo.IsInLineParagraph = true;
TextFragment secondParagraph = new TextFragment(" consectetur ");
secondParagraph.IsInLineParagraph = true;
Document document = new Document();
Page pdfPage = document.Pages.Add();
pdfPage.Paragraphs.Add(secondParagraphBoldPartOne);
pdfPage.Paragraphs.Add(secondParagraph);
pdfPage.Paragraphs.Add(secondParagraphBoldPartTwo);

@asad.ali Thanks for your Help.It runs well

1 Like