Products Technologies Demo Docs Blog Support Company
TX Text Control 34.0 SP1 has been released - Learn more

How to Manipulate the Document using the WebSocketHandler

The WebSocketHandler acts as a proxy between the client-side JavaScript and the TCP synchronization service. However, the WebSocketHandler can be used directly on the server side to do document manipulation.

How to Manipulate the Document using the WebSocketHandler

As of 32.0 SP1, the Document Editor has the new JavaScript property connectionID that returns the unique ID of the WebSocket connection. The WebSocketHandler acts as a proxy between the client-side JavaScript and the TCP synchronization service. This id is used for communication between the editor and the server-side WebSocketHandler for document rendering synchronization.

Now that this ID is publicly available, you can use it to connect to the document instance on the server side and manipulate the document directly in the WebSocketHandler in server-side C# code.

The following JavaScript code shows how to retrieve the connection ID and store it globally.

var connectionID;

 TXTextControl.addEventListener("textControlLoaded", function () {
     connectionID = TXTextControl.connectionID;
 });

Loading Documents

In the first example, a document is loaded directly from the server using the WebSocketHandler. The example consists of a button that calls the loadDocument function.

@using TXTextControl.Web.MVC

@Html.TXTextControl().TextControl().Render()

<input type="button" onclick="loadDocument()" value="Load Document" />

<script>

    var connectionID;

    TXTextControl.addEventListener("textControlLoaded", function () {
        connectionID = TXTextControl.connectionID;
    });

    function loadDocument() {
      $.ajax({
              url: '@Url.Action("LoadDocument")',
              type: 'POST',
              data: { connectionID: connectionID },
      });
    }
    
</script>

The method posts the connection ID to the LoadDocument endpoint. The GetInstance method of the WebSocketHandler returns the instance specified by the connection id. The LoadText method is then used to load the document directly into the instance.

[HttpPost]
public HttpResponseMessage LoadDocument(string ConnectionID)
{
        // connect the WebSocketHandler with the ConnectionID
        WebSocketHandler wsHandler = WebSocketHandler.GetInstance(ConnectionID);

        // the document directly server-side
        wsHandler.LoadText("App_Data/demo.tx", StreamType.InternalUnicodeFormat);

        return new HttpResponseMessage()
        {
                StatusCode = HttpStatusCode.OK
        };
}

Direct loading of a document is shown in the following screen video.

Loading using WebSocketHandler

Complex Formatting

For complex formatting tasks, the JavaScript API may be too slow (callbacks). In the following example, we want to highlight the keyword TextControl in a selected area of text.

Formatting using WebSocketHandler

The doComplexFormatting function calls the DoComplexFormatting server-side method. Before the Http POST method is called, the editor is set to read-only, and upon successful execution, the editor is set back to edit mode.

@using TXTextControl.Web.MVC

@Html.TXTextControl().TextControl().Render()

input type="button" onclick="doComplexFormatting()" value="Complex Formatting" />

<script>

    var connectionID;

    TXTextControl.addEventListener("textControlLoaded", function () {
        connectionID = TXTextControl.connectionID;
    });

    function doComplexFormatting() {

        TXTextControl.editMode = TXTextControl.EditMode.ReadOnly;

        $.ajax({
            url: '@Url.Action("DoComplexFormatting")',
            type: 'POST',
            data: { connectionID: connectionID },
            success: function (data) {
                TXTextControl.editMode = TXTextControl.EditMode.Edit;
            }
        });
    }
</script>

The selection is loaded into a temporary ServerTextControl for manipulation. Found keywords are highlighted in a loop before the selection is saved and loaded back into the editor.

[HttpPost]
public HttpResponseMessage DoComplexFormatting(string ConnectionID)
{
    // connect the WebSocketHandler with the ConnectionID
    WebSocketHandler wsHandler = WebSocketHandler.GetInstance(ConnectionID);

    byte[] data;

    // save the current selection to a byte array
    wsHandler.Selection.Save(out data, BinaryStreamType.InternalUnicodeFormat);

    // load the byte array into a ServerTextControl
    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();
        tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        // loop through all words and format the word "TextControl"
        for (int i = 0; i < tx.Text.Length; i++)
        {
            tx.Select(i, 1);
            tx.SelectWord();

            if (tx.Selection.Text == "TextControl")
            {
                tx.Selection.Bold = true;
                tx.Selection.Italic = true;
                tx.Selection.ForeColor = Color.Red;

                i += tx.Selection.Text.Length;
            }
        }

        // save the document to a byte array and load it into the WebSocketHandler
        tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
        wsHandler.Selection.Load(data, BinaryStreamType.InternalUnicodeFormat);
    }

    return new HttpResponseMessage()
    {
        StatusCode = HttpStatusCode.OK
    };
}

Conclusion

The WebSocketHandler can be used to directly manipulate the document on the server side using C# code. This is a very sufficient way to edit documents, especially for complex formatting tasks.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETWindows FormsWPF

TX Text Control 34.0 SP1 is Now Available: What's New in the Latest Version

TX Text Control 34.0 Service Pack 1 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETASP.NET CoreConference

Scaling TX Text Control Document Editor Applications

Learn how to scale TX Text Control Document Editor applications effectively for enhanced performance and user experience. A practical guide for high performance architectures.


ASP.NETASP.NET CoreConference

Text Control at DDC 2025: Bringing Next-Generation Document Technology to…

This week, we exhibited at the DDC 2025 conference in Cologne. It's a small but important event for the .NET community in the German-speaking world. For us at Text Control, it was an opportunity…


ASP.NETASP.NET CorePDF

Validate Digital Signatures and the Integrity of PDF Documents in C# .NET

Learn how to validate digital signatures and the integrity of PDF documents using the PDF Validation component from TX Text Control in C# .NET. Ensure the authenticity and compliance of your…


ASP.NETASP.NET CoreC#

Day-1 Support for .NET 10 in TX Text Control 34.0

Microsoft has officially released .NET 10. TX Text Control 34.0 offers day-one support for .NET 10 and has undergone thorough testing to ensure compatibility with the latest .NET version and…

Summarize this blog post with:

Share on this blog post on: