Products Technologies Demo Docs Blog Support Company

This blog post contains outdated information.

The cited code snippets may be workarounds, and be part of the official API in the meantime.

JavaScript API: Working with Merge Fields

This article gives an overview of how to add, remove and manipulate merge fields programmatically using the JavaScript API.

JavaScript API: Working with Merge Fields

The HTML5-based document editor, that is part of the product TX Text Control .NET Server, comes with a JavaScript API to manipulate the most typical elements in a document.

This JavaScript API can be used with the ASP.NET WebForms, MVC and also the language independent Widget version which is currently in beta. This article gives an overview of how to insert, remove and manipulate merge fields programmatically.

Inserting Merge Fields

In order to insert a merge field, a new Javascript: TXTextControl.MergeField object needs to be created:

var mergeField = new TXTextControl.MergeField();
mergeField.name = "company";
mergeField.text = "[company]";

This field can be added to the document at the current input position using the Javascript: TXTextControl.addMergeField method:

TXTextControl.addMergeField(mergeField);

If you would like to insert the merge field at a specific location, you can set the input position using the Javascript: Selection object first:

var bounds = { "start":5,"length":0 };
TXTextControl.selection.setBounds(bounds);

Deleting Merge Fields

Each time, merge fields (or any text field) should be manipulated, a new synchronized list of fields needs to be retrieved from Text Control. This is done using the Javascript: TXTextControl.getTextFields method which returns a collection of either all text fields or only the text field at the current input position.

The following code deletes the merge field at the current input position:

TXTextControl.getTextFields(function(e) {
  if(e[0] != undefined)
    TXTextControl.removeTextField(e[0])
}, true);

Manipulating Merge Fields

The following code returns all available merge fields in the current text part. The method getTextFields returns all text fields. In order to filter the merge fields, the type and the typeName properties need to be checked:

var mergeFields = [];

TXTextControl.getTextFields(function(e) {
        e.forEach(function(e) {
          if(e.type == "APPLICATIONFIELD" &&
                  e.typeName == "MERGEFIELD") {
                          mergeFields.push(e);
                  }
        });
});

console.log(mergeFields);

Now, these fields can be manipulated. The following code sets the text of the first field in the created array of fields:

mergeFields[0].text = "New text";

The following sample uses the Widget to show how a merge field can be added and removed from the document:

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • Javascript: Selection Object
  • Javascript: TXTextControl.addMergeField Method
  • Javascript: TXTextControl.getTextFields Method
  • Javascript: TXTextControl.MergeField Object

Reporting

The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.

See Reporting products

Related Posts

AngularJavaScriptReact

Technology Preview: Embeddable HTML Widget to integrate Document Editing to…

This technology preview shows an early version of an HTML widget that can be embedded into any HTML page.


ASP.NETReportingHTML5

Creating Your First ASP.NET Reporting Application

This tutorial shows how to use the MailMerge component in an ASP.NET Web application to merge a template with data to create an Adobe PDF document.


ASP.NETReportingAuto Save

Automatically Reconnect to the Server and Recover the Document

We just published a sample project that shows how to reconnect to the server and how to recover the current document.


JavaScriptReporting.NET Core

Edit Templates in an ASP.NET Core MVC Application (.NET Core)

We published a sample project to GitHub that shows how to create a .NET Core MVC application to edit templates using the ReportingCloud editor widget.


JavaScriptReportingReportingCloud

ReportingCloud Editor Widget Released to Beta

We released an editor widget to beta that can be embedded into any HTML page just by adding a JavaScript tag.