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.

Using TX Text Control .NET Server in Blazor Server Apps

The TX Text Control document editor and the server-side non-UI component ServerTextControl can be used in Blazor Server Apps. This article shows how to save documents to the server and how to modify them using the ServerTextControl class.

Using TX Text Control .NET Server in Blazor Server Apps

Since TX Text Control .NET Server 30.0 supports .NET 5 and .NET 6, the components can be used in Blazor Server Apps. This article shows how to save a document using the document editor and to modify the document server-side using the ServerTextControl class.

Data Flow

The following dialog shows the data flow in this concept. The JavaScript API of TX Text Control is used to save the document which is transferred to .NET (the "server"). In .NET, the document is processed using an instance of ServerTextControl and is sent back to client-side by invoking a JavaScript function that loads the content back into the TX Text Control document editor.

TX Text Control in Blazor

Server-side, the document is loaded and modified by adding the text "This document has been modified by .NET" to the beginning of the document before it is loaded back into the editor.

TX Text Control in Blazor

The Required Code

The following code shows the editor page that consists of the document editor and a button:

@page "/editor"
@inject IJSRuntime JSRuntime

<h1>Send Document to .NET</h1>

<style>
        
        #txDocumentEditorContainer {
            width: 1100px;
            height: 600px;
            display: inline-block;
            position: relative;
        }
        
</style>
        
<div id="txDocumentEditorContainer" @ref="txDocumentEditorContainer"></div>

<br />

<button @onclick="SaveDocument">
        Save and Reload Document
</button>

@code
{
        private ElementReference txDocumentEditorContainer;

        protected override async Task OnAfterRenderAsync(bool firstRender) {
                if (firstRender) {
                        await JSRuntime.InvokeVoidAsync("addEditorToElement", txDocumentEditorContainer);
                }
        }

        private void SaveDocument()
        {
                // create a .NET object and call the 'saveDocument' JS function
                var dotNetReference = DotNetObjectReference.Create(this);
                JSRuntime.InvokeVoidAsync("TextControl.saveDocument", dotNetReference);
        }

        [JSInvokable("ProcessDocument")]
        public void ProcessDocument(string document)
        {
                byte[] bDocument;

                // create a ServerTextControl instance to load the saved document
                using (TXTextControl.ServerTextControl tx = new  TXTextControl.ServerTextControl()) {
                        tx.Create();
                        tx.Load(Convert.FromBase64String(document), TXTextControl.BinaryStreamType.InternalUnicodeFormat);
                        
                        // add additional text to the document
                        tx.Selection.Text = "This document has been modified by .NET\r\n";
                        
                        // save back
                        tx.Save(out bDocument, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
                }

                // invoke the JS function 'loadDocument' to load back to the modified document
                JSRuntime.InvokeVoidAsync("TextControl.loadDocument", bDocument);
        }

}

When clicking the button, a DotNetObjectReference object is created and invoked with the TextControl.saveDocument JavaScript method. In this method, the document is saved and passed to the .NET method ProcessDocument (see code above).

var TextControl = TextControl || {};

TextControl.saveDocument = function (dotNetObject) {

        // save the document on TXTextControl object
        TXTextControl.saveDocument(TXTextControl.StreamType.InternalUnicodeFormat, function (document) {
                // call the .NET method 'ProcessDocument' with the saved document data
                dotNetObject.invokeMethodAsync('ProcessDocument', document.data);
        });

};

TextControl.loadDocument = function (document) {

        // load the document back into the editor (TXTextControl)
        TXTextControl.loadDocument(TXTextControl.StreamType.InternalUnicodeFormat, document);

};

In the .NET method ProcessDocument, the saved document is loaded into a ServerTextControl instance to add additional text. Finally, it is saved back by invoking another JavaScript method TextControl.loadDocument to load the modified document into the document editor.

Conclusion

This data flow shows how to use the document editor and the server-side non-UI components of TX Text Control in combination in Blazor Server Apps.

You can test this data flow using the sample project that is hosted in our GitHub account.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

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

  • TX Text Control .NET Server 30.0
  • DS Server (trial sufficient)
  • Visual Studio 2022

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.NETBlazorJavaScript

Using DS Server with Blazor

This sample and article shows how to integrate the DS Server DocumentEditor using JavaScript into an ASP.NET Blazor application. Blazor allows you create interactive web applications using C#.


AngularASP.NETJavaScript

JavaScript: Avoid Flickering and Visual Updates by Grouping Undo Steps

The JavaScript API can be used to group several steps into one undo action that can be undone with a single undo call. Additionally, those groups are visually updated at once to avoid screen…


ASP.NETJavaScriptDeployment

DS Server or TX Text Control? Different Deployment Scenarios

The Text Control document processing technology can be deployed in various ways using different products for different applications. This overview explains the differences and deployment strategies.


ASP.NETJavaScriptDS Server

Document Editor: JavaScript Object Availability and Order of Events

The online document editor, available for MVC and Angular, provides a JavaScript API to manipulate the document and the style and behavior of the editor. This article explains the order of events…


AngularASP.NETJavaScript

Track Changes: Show 'Original' and 'No Markup'

The redlining feature is very helpful when working on the same document with multiple authors specifically with legal or healthcare documents where changes need to be tracked and safely logged.…