ASP.NET Rich Text Editor - Using DocVariable to Add Custom Data and Charts (Shipping in v15.1)

11 June 2015

In the "What's new for ASP.NET v15.1" webinar, I showed the mail merge like features of the new DevExpress ASP.NET Rich Text Editor. Click the image below to see the webinar video:

In this blog post, I'll explain some more details about this great new functionality.

Fields Support

In the DevExpress ASP.NET Rich Text Editor (ASPxRichEdit), document fields are special placeholders for non-static data that might change (be updated on field update). These placeholders are replaced with actual data when the document is rendered for display or print. Using fields, you can automate different aspects of your document, such as auto page numbering, inserting actual dates and times, etc. The default Mail Merge ribbon tab can be used to work with fields (create, update, switch between field display modes). There are six field codes:

  • DATE - Inserts the current date and time.
  • TIME - Inserts the current time.
  • HYPERLINK - Enables you to navigate to another location or to a bookmark.
  • NUMPAGES - Inserts the total number of pages.
  • PAGE - Inserts the number of the page containing the field.
  • DOCVARIABLE - Enables you to programmatically insert complex content when this field is updated.

Docvariable

The DevExpress ASP.NET Rich Editor (ASPxRichEdit) enables you to store custom information in the document using document variables. To insert a document variable in the document, use the DOCVARIABLE field code.

The DOCVARIABLE field code has the following syntax:

{ DOCVARIABLE "variable name" "argument1" "argument 2"... }

Argument1, Argument2, etc. are optional parameters.

How to insert a DOCVARIABLE field

To add the Docvariable field code to your RTF doc:

  1. Press Ctrl+F9 to insert a new field
  2. write the DOCVARIABLE keyword, space and a name of a variable

Calculate the field value

To calculate a value of this field in ASPxRichEdit:

First, override the CalculateDocumentVariable event.

In the handler, you can calculate a field result. Get a variable name and arguments (if they are defined) from the event args. Then set the Handled and Value properties. The Value property can have the string or Document types. If it’s set with a string then this string will be inserted as a field result.

If you want to insert some advanced content, you can set the Value property with a Document instance. So, other rich-text document can be inserted as a field result in a current rich-text document.

Here's an example:

protected void DemoRichEdit_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) {
        switch(e.VariableName) {
            case "Chart":
                var sales = GetSales(e.Arguments[0].Value);
                DocumentImageSource chart = DocumentImageSource.FromStream(CreateChart(sales));
                RichEditDocumentServer srv = new RichEditDocumentServer();
                srv.Document.Images.Append(chart);
                e.Value = srv.Document;
                e.Handled = true;
                break;
            case "CommonSales":
                var commonSales = GetCommonSales(e.Arguments[0].Value);
                e.Value = commonSales.ToString("C");
                e.Handled = true;
                break;
            default:
                break;
        }
    }

Demo

Try this feature online now:

DevExpress ASP.NET RichEdit demo - Document Variables Support

Thanks.

Your Next Great .NET App Starts Here

Year after year, .NET developers such as yourself consistently vote DevExpress products #1.

Experience the DevExpress difference for yourself and download a free 30-day trial of all our products today: DevExpress.com/trial (free support is included during your evaluation).

no comments
No Comments

Please login or register to post comments.