Skip to main content

How to: Insert Dynamic Content

  • 3 minutes to read

The code sample below shows how use the DOCVARIABLE field and RichEditControl.CalculateDocumentVariable event to insert dynamic (formatted) content. In this example, the DOCVARIABLE field is used to display the DevExpress community website’s RSS feed.

IMAGE

Note

A complete sample project is available at Dynamic Content demo module.

  1. Load a document template, if required.
  2. Insert a DOCVARIABLE field. Use the FieldCollection.Create method, as shown below:

    private static void InsertDocVariableField(Document document)
    {
        document.Fields.Create(document.Range.End, "DOCVARIABLE rssFeed");
    }
    
  3. Use the RichEditDocumentServer object to generate or load the target dynamic content. In this example, the GenerateRssFeed method returns this RichEditDocumentServer object.

    private static RichEditDocumentServer GenerateRssFeed()
    {
        RichEditDocumentServer rssProcessor = new RichEditDocumentServer();
        Document document = rssProcessor.Document;
    
        //Create a new bulleted list
        AbstractNumberingList abstractNumberingList = document.AbstractNumberingLists.BulletedListTemplate.CreateNew();
        document.NumberingLists.CreateNew(abstractNumberingList.Index);
    
        SyndicationFeed feed = null;
    
        //Extract feed items
        try
        {
            using (XmlReader reader = XmlReader.Create("https://community.devexpress.com/blogs/MainFeed.aspx"))
            {
                feed = SyndicationFeed.Load(reader);
            }
        }
        catch
        {
            return null;
        }
    
        //Format each feed item into the bullet list item
        document.BeginUpdate();
        foreach (SyndicationItem item in feed.Items)
            //This method inserts each item as a hyperlink with publication date
            AddSyndicationItem(document, item);
        document.EndUpdate();
        return rssProcessor;
    }
    
  4. Handle the RichEditControl.CalculateDocumentVariable event. Use the RichEditDocumentServer object created in the previous step as the CalculateDocumentVariableEventArgs.Value property value. Identify the field by the CalculateDocumentVariableEventArgs.VariableName property value.

    Note

    The current DOCVARIABLE field value may be lost if you update this field in another word processing application, where the CalculateDocumentVariable event isn’t used. Use one of the following approaches to suppress DOCVARIABLE field updates:

    void richEditControl_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e)
    {
        //Retrieve the field by its ID
        if (e.VariableName == "rssFeed")
        {
            //Retain the last paragraph of the inserted document in the resulting document
            e.KeepLastParagraph = true;
    
            //Use the method result as the field's value
            e.Value = GenerateRssFeed();
            if (e.Value != null)
    
                //Keep the current field value
                e.Handled = true;
    
                //Suppress the field update
                e.FieldLocked = true;
        }
    
    }
    
  5. Call the FieldCollection.Update to update all document fields.

    public DynamicContentModule()
    {
        //This method specifies security protocols for RSS feed
        SetupSecurityProtocol();
    
        //Load a document template
        richEditControl.LoadDocument("Dynamic Content.docx");
    
        //Insert the DOCVARIABLE field
        InsertDocVariableField(richEditControl.Document);
    
        //Subscribe to the CalculateDocumentVariable event
        richEditControl.CalculateDocumentVariable += richEditControl_CalculateDocumentVariable;
    
        //Update all document fields
        richEditControl.Document.Fields.Update();
    }