Skip to main content

Working with Rich Text Fields

This section explains how to work with rich text fields.

Reference

Setting Values

Set the value of a rich text field using the SetRichTextField method of the IModel object. Rich text fields can have values in multiple formats. When edited in the application, the value is set in HTML and text format.

public void SetRichTextField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;

//Specifying "html" as the format automatically sets the value in "text" format.
var htmlText = $"<body>....</body>";
model.SetRichTextField("Field1", htmlText, "html");
}
info
  • If you specify HTML as the format using the SetRichTextField method above, the text value will be automatically set along with the HTML value.
  • If response performance is important, use the SetRichTextFieldValues method and specify both the HTML value and the text value as arguments.

Setting an Image

To set an image in a rich text field, convert the image byte array to a string, and then set the value defined by that string to the rich text field using the SetRichTextField method of the IModel object.

using System.IO;
using System.Drawing;

public void SetImageToRichText(ICommandContext c, ICommandParams p)
{
//Read the specified file into a byte array.
var filePath = c.App.Window.UI.ShowOpenFileDialog("Open", "Image Files|*.png");
if (filePath == null)
{
return;
}
var bytes = File.ReadAllBytes(filePath);

//Convert the read byte array into a BASE64 binary string.
var base64 = System.Convert.ToBase64String(bytes);

//Convert the read byte array into an Image to obtain the image width and height.
var stream = new MemoryStream(bytes);
var image = Image.FromStream(stream);

//Define an HTML format value using the converted BASE64 binary string and the Image width and height.
var htmlText = $"<body><p><img width=\"{image.Size.Width}\" height=\"{image.Size.Height}\" src=\"data:image/png;base64,{base64}\"/></p></body>";

//Set the value of the rich text field of the selected model.
IModel model = c.App.Workspace.CurrentModel;
model.SetRichTextField("Field1", htmlText, "html");
}
info
  • System.IO is referenced when reading the specified file into a byte array or converting the byte array to stream format.
  • System.Drawing is referenced when converting a stream format file to an image.

Getting the Value

To get the rich text field value of a model, use the GetRichTextField method of the IModel object.

public void GetRichTextField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;

//Get the simple text
var text = model.GetRichTextField("Field1", "text");

//Get the HTML
var html = model.GetRichTextField("Field1", "html");

//...
}

Get an image

To get an image from a rich text field, extract the image string from the rich text field value obtained by the GetRichTextField method of the IModel object, and convert that string to a byte array.

using AngleSharp.Html.Parser;

public void GetImageFromRichText(ICommandContext c, ICommandParams p)
{
//Get the rich text field value of the selected model.
IModel model = c.App.Workspace.CurrentModel;
var htmlText = model.GetRichTextField("Field1", "html");

//Parse HTML and extract the img tag.
var htmlDocument = new HtmlParser().ParseDocument(htmlText);
var imgElement = htmlDocument.GetElementsByTagName("img").FirstOrDefault();
if (imgElement == null)
{
return;
}

//Obtain the BASE64 binary string from the extracted img tag.
//The src attribute of the extracted img tag is "data:image/png;base64,~",
//The actual BASE64 binary string is the string after the ",".
var srcAttribute = imgElement.GetAttribute("src");
var base64 = srcAttribute.Substring(srcAttribute.IndexOf(",") + 1);

/ //Convert the obtained BASE64 binary string to a byte array.
var bytes = System.Convert.FromBase64String(base64);

/ //...
}
info
  • When parsing HTML, reference AngleSharp.Html.Parser.

Get the format

To get the rich text format stored in a field, use the GetRichTextFieldFormats method of the IModel object.

public void GetRichTextFieldFormats(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;

IEnumerable<string> formats = model.GetRichTextFieldFormats("Field1");

//...
}