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

Free Support Forum - aspose.com

Append 2 TextFragments and add to Cell

I would like to achieve the below style when generating a table in a PDF


First Name: John Doe

I created 2 TextFragment classes one is bold and other is normal. What is the code so that these two TextFragments gets appended and added to the cell?

var nameInfoCell = row.Cells.Add();
nameInfoCell.ColSpan = 8;

var firstNameLabelTextFragment = new TextFragment("First Name: ");
firstNameLabelTextFragment.TextState.FontStyle = FontStyles.Bold;

var firstNameValueTextFragment = new TextFragment(“John Doe”);

var textBuilder = new TextBuilder(page);
textBuilder.AppendText(firstNameLabelTextFragment);
textBuilder.AppendText(firstNameValueTextFragment);

Hi Ronaldo,


Thanks for contacting support.

In order to accomplish your requirement, you may consider creating two separate TextSegment instances where you can set the formatting information against individual segment object and then add both segments to segments collection of TextFragment object. Later on, the TextFragment object can be added to paragraphs collection table cell instance. Please try using following code snippet.

For your reference, I have also attached the PDF document generated over my end.

[C#]

// Source PDF file<o:p></o:p>

Document doc = new Document();

doc.Pages.Add();

Aspose.Pdf.Table table = new Aspose.Pdf.Table();

table.DefaultColumnWidth = "100";

Aspose.Pdf.Row row = table.Rows.Add();

var nameInfoCell = row.Cells.Add();

// nameInfoCell.ColSpan = 8;

var firstNameLabelTextFragment = new TextFragment();// ("First Name: ");

Aspose.Pdf.Text.TextSegment segment1 = new TextSegment();

firstNameLabelTextFragment.Segments.Add(segment1);

segment1.Text = "First Name";

segment1.TextState.FontStyle = FontStyles.Bold;

// firstNameLabelTextFragment.TextState.FontStyle = FontStyles.Bold;

var segment2 = new Aspose.Pdf.Text.TextSegment(" John Doe");

firstNameLabelTextFragment.Segments.Add(segment2);

//var textBuilder = new TextBuilder(page);

//textBuilder.AppendText(firstNameLabelTextFragment);

//textBuilder.AppendText(firstNameValueTextFragment);

nameInfoCell.Paragraphs.Add(firstNameLabelTextFragment);

doc.Pages[1].Paragraphs.Add(table);

string outFile = "c:/pdftest/TextFormatting_Test_TableCell.pdf";

// Save the output in HTML format

doc.Save(outFile);

but this adds both textsegment in the same line. However adding line break using “\r\n” ignores the text formatting. Please tell me how to add line break in textFragment without loosing the font size and styles.