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.

Embedding TXTextControl.Web in non-.NET Framework applications like .NET Core and AngularJS

This article shows how to embed the ASP.NET MVC component TXTextControl.Web into non-.NET Framework applications.

Embedding TXTextControl.Web in non-.NET Framework applications like .NET Core and AngularJS

TX Text Control .NET Server comes with a true WYSIWYG HTML5-based editor for ASP.NET MVC and Web Forms. But it can be embedded into other application types as well.

IFrame elements are designed to allow you to embed other web documents into your document. This is widely used to embed third party content into your website such as social media widgets. Using an IFrame, the ASP.NET-based editor can be easily embedded into any non-.NET based web applications such as .NET Core or AngularJS based web sites.

The following diagram shows this structure:

SendMessage Diagram

The sample demo consists of a hosting .NET Core web application and an ASP.NET MVC application that hosts the TX Text Control editor. The simple .NET Core website hosts an IFrame element that points to the ASP.NET MVC application:

@{
    ViewData["Title"] = "Home Page";
}

<div class="row">
    <div class="col-md-8 editor">
        
        <iframe id="TXTextControl" src="http://localhost:2957/"></iframe>
        
        <input type="button" value="Load Selection" onclick="SendMessage()" />

    </div>
</div>

<script>
    function SendMessage() {
        var receiver = document.getElementById('TXTextControl').contentWindow;
        var message = `{"txtextcontrol": {"method": "loadDocument",
            "parameter": "This is some <b>HTML</b> text."
            }}`;

        receiver.postMessage(message, 'http://localhost:2957/');
    }
</script>

When the button is clicked, JavaScript is used to send a message to the source of the IFrame. The window.postMessage() method safely enables cross-origin communication. This method, when called, causes a MessageEvent to be dispatched at the target window.

The receiving JavaScript catches this message in order to process it:

@using TXTextControl.Web
@using TXTextControl.Web.MVC

@Html.TXTextControl().TextControl(settings =>
{
    settings.Dock = DockStyle.Window;
}).Render()

<script>
    window.onload = function () {

        function receiveMessage(e) {
          
            if (e.origin !== "http://localhost:3053")
                return;

            var message = JSON.parse(e.data);

            if (message.txtextcontrol == null)
                return;

            switch (message.txtextcontrol.method) {
                case "loadDocument": {
                    var encoded = btoa(message.txtextcontrol.parameter); 
                    TXTextControl.loadSelection(TXTextControl.StreamType.HTMLFormat, encoded);
                }
             }
        }

        window.addEventListener('message', receiveMessage);
    }
</script>

In this demo, an HTML string should be loaded into the TX Text Control editor. As it is only possible to send a message to the receiving IFrame, a protocol is required to structure the request. We use the following structure:

{
   "txtextcontrol":{
      "method":"loadDocument",
      "parameter":"This is some <b>HTML</b> text."
   }
}
Element Description
txtextcontrol Make sure that this message is for TX Text Control
method The name of the method
parameter The parameter to be used for the method

In the receiving JavaScript, a switch statement is used to process various methods. In this demo, only one method is processed which is supposed to load a formatted HTML string using the TX Text Control Javascript: TXTextControl.loadSelection method.

You can test this on your own by cloning the sample GitHub project.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • Javascript: TXTextControl.loadSelection Method

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2017 or better
  • TX Text Control .NET Server (trial sufficient)

Angular

Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.

Learn more about Angular

Related Posts

AngularReportingBASTA

Integrate Documents in any Platform: Visit us at BASTA! 2018

This month, we will be exhibiting at BASTA! 2018 in Mainz to show our solutions to integrate documents in any platform.


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.


AngularJavaScriptReporting

Getting Started: Angular Document Editor Attributes Explained

This article explains the possible attributes that can be used when initializing the TX Text Control Document Editor for Angular.


AngularJavaScriptReporting

Getting Started: Programming the Angular Document Editor using JavaScript

This tutorial shows how to use the JavaScript API to program the TX Text Control Angular Document Editor.


AngularJavaScriptReporting

Getting Started: Loading and Saving Documents using Angular

This tutorial shows how to load and save documents using the JavaScript API in Angular applications.