How to: Insert Dynamic Content

  • 4 minutes to read

The code sample below shows how use the DOCVARIABLE field and RichEditDocumentServer.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

  1. Create a new RichEditDocumentServer object and 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 another 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 RichEditDocumentServer.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:

    private static void WordProcessor_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, and save the result.

    The Main method should look as follows:

    static void Main(string[] args)
    {
        //This method specifies security protocols for RSS feed
        SetupSecurityProtocol();
    
        RichEditDocumentServer wordProcessor = new RichEditDocumentServer();
    
        //Load a document template
        wordProcessor.LoadDocument("Dynamic Content.docx");
    
        //Insert the DOCVARIABLE field
        InsertDocVariableField(wordProcessor.Document);
    
        //Subscribe to the CalculateDocumentVariable event
        wordProcessor.CalculateDocumentVariable += WordProcessor_CalculateDocumentVariable;
    
        //Update all document fields
        wordProcessor.Document.Fields.Update();
    
        //Save the result and open the file
        wordProcessor.SaveDocument("MergedDocument.docx", DocumentFormat.OpenXml);
        Process.Start(new ProcessStartInfo("MergedDocument.docx") { UseShellExecute = true });
    }