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.
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);
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
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));