How to add Rich Text format text to the cell?

I have a C# object, need to add Rich-text content to the cell, but it doesn’t work for me, it seems the API only takes string content that doesn’t allow formatting of the text.

Here is a snip of the code:

TableCell myCell = new TableCell(document) { CellFormat = { BackgroundColor = new Color(255, 255, 255), WrapText = true } };
myCell.Blocks.Add(
    new Paragraph(document,
        new Run(document, strRichTextContent) { CharacterFormat = { FontName = CONTENT_FONT_STYLE, Size = 11, Bold = false } }));

Hi,

What value does your strRichTextContent have?

Anyway, a Paragraph can have multiple Run elements, and each of them can have its own formatting.
For example:

var document = new DocumentModel();

var myCell = new TableCell(document);
myCell.CellFormat.BackgroundColor = new Color(255, 255, 255);
myCell.CellFormat.WrapText = true;

var myParagraph = new Paragraph(document);
myCell.Blocks.Add(myParagraph);

var myRun = new Run(document, "Red Text");
myParagraph.Inlines.Add(myRun);
myRun.CharacterFormat.FontColor = Color.Red;

myRun = new Run(document, "Blue Text");
myParagraph.Inlines.Add(myRun);
myRun.CharacterFormat.FontColor = Color.Blue;

Or you can add HTML or RTF text content, like this:

var myCell = new TableCell(document) { CellFormat = { BackgroundColor = new Color(255, 255, 255), WrapText = true } };
var myParagraph = new Paragraph(document);
myCell.Blocks.Add(myParagraph);
myParagraph.Content.LoadText("<span style='color:red'>Red Text</span><span style='color:blue'>Blue Text</span>", LoadOptions.HtmlDefault);

I hope this helps.

Regards,
Mario

Thank you!

The rich text content looks like “aaa\n bbb\n ccc\n\r”, and I tried LoadOptions.HtmlDefault doesn’t seem to work out.

While I am trying to use RtfLoadOptions.RtfDefault, it gives error message of “Provided file is not a valid RTF file.” but we didn’t even load the content from file

Actually, I am able to get it work by using the richTextControl
Thank you!

Try this:

string strRichTextContent = "aaa\n bbb\n ccc\n\r";
var myParagraph = new Paragraph(document, strRichTextContent );

This paragraph constructor will create the required inline elements for you.
In other words, it will do something like this:

var myParagraph = new Paragraph(document,
    new Run(document, "aaa"),
    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
    new Run(document, " bbb"),
    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
    new Run(document, " ccc"),
    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
    new SpecialCharacter(document, SpecialCharacterType.LineBreak));

Thank you for your comments.