Products Technologies Demo Docs Blog Support Company

Use and Create Reusable Formatted Text Snippets to Build Documents in ASP.NET Core C#

This sample implements a document builder that uses text snippets to create documents fast and efficient. Additionally, text snippets can be created and modified.

Use and Create Reusable Formatted Text Snippets to Build Documents in ASP.NET Core C#

Manual document creation and processing is a high-effort process and error-prone. Information is manually typed into documents, older versions are reused (copy and change of existing documents) or documents are created from scratch each time a new document is required.

Contract Lifecycle Challenges

Specifically for business critical documents such as contracts and agreements, manual processes can be dangerous in many ways.

The following table lists typical challenges with agreement processes:

Preparation Collaboration Signing Managing
Errors due to manually entered data into contracts. Usage of different "red-lining" tools and versions (MS Word). No central portal to see status of the signing process. Errors due to manually entered data into contracts.
Delays due to long preparation process. No central control over tracked changes. Delays due to missing information. No central contract repository or search.
No data binding to CRM and ERP systems. Poor user experience. Delays due to wrong routing. Security and data privacy

For this sample, we take a look at the Preparation column and specifically the first reason:

Errors due to manually entered data into contracts.

To avoid this, reusable text snippets should be used and maintained at a central location. The following screenshot shows the sample that allows users to insert formatted snippets.

Text Snippets in TX Text Control

Creating Snippets

The interface allows users to create new snippets by selecting any text in the document and clicking New Snippet.

Text Snippets in TX Text Control

All elements are saved within the snippet including formatted text, merge and form fields, tables and other document elements. Technically, the snippet is saved using the Selection.save method and is stored server-side along with an HTML preview generated by TX Text Control.

[HttpPost]
public bool Create(string document, string name) {

   var snippetName = _applicationSettings.SnippetDirectory +
      "/" + Path.GetInvalidFileNameChars().Aggregate(name, (f, c) => f.Replace(c, '_').Replace(' ', '_'));

   using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
      tx.Create();

      // load the snippet
      tx.Load(Convert.FromBase64String(document), TXTextControl.BinaryStreamType.InternalUnicodeFormat);

      // select 200 chars as a preview
      tx.Select(0, 200);

      // save the body of the snippet
      TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() {
         CssSaveMode = TXTextControl.CssSaveMode.Inline
      };

      tx.Selection.Save(out string htmlString, TXTextControl.StringStreamType.HTMLFormat, saveSettings);

      TextReader reader = new StringReader(htmlString);
      HtmlDocument doc = new HtmlDocument();
      doc.Load(reader);
      HtmlNode bodyNode = doc.DocumentNode.SelectSingleNode("/html/body");
      System.IO.File.WriteAllText(snippetName + ".htm", bodyNode.InnerHtml);
   }

   // store the actual snippet in the Text Control format
   System.IO.File.WriteAllBytes(snippetName + ".tx", Convert.FromBase64String(document));

   return true;
}

Loading Snippets

In case a snippet is inserted into the document, the Selection.load method is used to load the document asynchronously.

function getSnippet(snippetName, snippetFormat) {

    var serviceURL = "@Url.Action("Content", "Snippets")";

    console.log(snippetName);

    $.ajax({
        type: "POST",
        url: serviceURL,
        data: {
            name: snippetName,
            format: snippetFormat
        },
        success: successFunc,
        error: errorFunc
    });

    function successFunc(data, status) {
        if (snippetFormat == "HTM")
            document.querySelector('[tx-snippet-name="' + snippetName + '"] .snippet-preview').innerHTML = data + "...";
        else
            TXTextControl.selection.load(TXTextControl.StreamType.InternalUnicodeFormat, data);
    }

    function errorFunc() {
        $("#myToast .toast-body").text("Error! :-(");
        $("#myToast").toast("show");
    }
}

Managing Snippets

The sample also implements an overview page that lists all created snippets.

Text Snippets in TX Text Control

This section allows you to edit and delete existing snippets and to create new snippets from scratch.

Text Snippets in TX Text Control

Test this on your own and fork our sample project on GitHub.

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 31.0
  • Visual Studio 2022

Related Posts

ASP.NETASP.NET CoreDocument Editor

Getting Started Video Tutorial: Document Editor in ASP.NET Core C# on Linux

This video tutorial shows how to use the Document Editor in an ASP.NET Core application using C# and deploy on Linux using Docker. This tutorial is part of the TX Text Control Getting Started…


ASP.NETDocument EditorDocument Protection

Document Protection in ASP.NET with TX Text Control: Healthcare Use Cases

This article explores document protection use cases in the healthcare domain. It also explains how to assign usernames, configure edit modes, and use editable regions based on user roles using the…


ASP.NETApp ServicesASP.NET Core

Deploying the TX Text Control Document Editor in an ASP.NET Core Web App to…

This tutorial shows how to deploy the TX Text Control Document Editor to Azure App Services using an ASP.NET Core Web App. The Document Editor is a powerful word processing component that can be…


AngularASP.NETBlazor

Building an ASP.NET Core Backend (Linux and Windows) for the Document Editor…

This article shows how to create a backend for the Document Editor and Viewer using ASP.NET Core. The backend can be hosted on Windows and Linux and can be used in Blazor, Angular, JavaScript, and…


ASP.NETBlazorASP.NET Core

TX Text Control for Blazor: Mail Merge Integration Tutorial

This tutorial shows how to integrate the TX Text Control MailMerge component into a Blazor application using the TX Text Control .NET Server.