Skip to main content

Working with rich text fields

This section explains how to work with rich text fields.

Reference

Setting values

Use the SetRichTextField method of the IModel object to set the value of a rich text field. Values in multiple formats can be set in a rich text field. When you edit 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" format as the format also automatically sets the value in "text" format.
var htmlText = $"<body>....</body>";
model.SetRichTextField("Field1", htmlText, "html");
}
info
  • If you specify HTML format as the format in the above SetRichTextField method, the text format value will be automatically set along with the HTML format value.
  • If you prioritize response performance, use the SetRichTextFieldValues method and specify both the HTML format value and the text format value as arguments.

Setting an image

To set an image in a rich text field, convert the byte array of the image to a string, and 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 get the image width and height.
var stream = new MemoryStream(bytes);
var image = Image.FromStream(stream);

//Define the HTML format value using the converted BASE64 binary string and the width and height of the Image.
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
  • Refers to System.IO when reading the specified file into a byte array or converting the byte array into stream format.
  • Refers to System.Drawing when converting a stream format file into an image.

Get 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 simple text
var text = model.GetRichTextField("Field1", "text");

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

//...
}

Get image

To get an image from a rich text field, extract the image part of the string from the rich text field value obtained by the GetRichTextField method of the IModel object, and convert the 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 img tags. var htmlDocument = new HtmlParser().ParseDocument(htmlText); var imgElement = htmlDocument.GetElementsByTagName("img").FirstOrDefault(); if (imgElement == null) { return; } //Get the BASE64 binary string from the extracted img tag. //The src attribute of the extracted img tag is "data:image/png;base64,~", and the actual BASE64 binary string is the string after ",". 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, refer to `AngleSharp.Html.Parser`. ::: ## Get the format

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

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

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

//...
}