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

The TX Text Control JavaScript API enables inserting, deleting, and manipulating merge fields in the HTML5-based document editor. Developers create MergeField objects, add them at specific positions using the Selection object, and retrieve field collections via getTextFields.

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…

TX Text Control offers a technology preview of an embeddable JavaScript widget that adds document editing to Angular, React, and other web frameworks. Developers add a script tag and create a…


ASP.NETReportingHTML5

Creating Your First ASP.NET Reporting Application

The MailMerge and ServerTextControl components of TX Text Control .NET Server for ASP.NET enable server-side reporting in Web Forms. A template.docx merges with XML data via a button click…


ASP.NETReportingAuto Save

Automatically Reconnect to the Server and Recover the Document

When a WebSocket connection drops, the TX Text Control editor automatically reconnects and recovers the document using browser local storage. The implementation saves document state client-side…


JavaScriptReporting.NET Core

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

The ReportingCloud Editor Widget integrates with ASP.NET Core MVC to enable browser-based template editing. The cross-platform application lists templates from cloud storage, loads them into the…


JavaScriptReportingReportingCloud

ReportingCloud Editor Widget Released to Beta

The ReportingCloud editor widget beta embeds a fully featured document editor into any HTML page using a single JavaScript tag. Developers register their web hostname in the ReportingCloud Portal…

Share on this blog post on: