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

- **Author:** Bjoern Meyer
- **Published:** 2023-02-16
- **Modified:** 2025-11-16
- **Description:** This sample implements a document builder that uses text snippets to create documents fast and efficient. Additionally, text snippets can be created and modified.
- **4 min read** (625 words)
- **Tags:**
  - ASP.NET
  - Snippets
  - Document Editor
- **Web URL:** https://www.textcontrol.com/blog/2023/02/16/using-and-creating-reusable-formatted-text-snippets-to-build-documents/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/02/16/using-and-creating-reusable-formatted-text-snippets-to-build-documents/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/02/16/using-and-creating-reusable-formatted-text-snippets-to-build-documents/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TXTextControl.Core.Editor.Snippets

---

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](https://s1-www.textcontrol.com/assets/dist/blog/2023/02/16/a/assets/snippet1.gif "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/02/16/a/assets/snippet2.gif "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/02/16/a/assets/snippet3.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/02/16/a/assets/snippet4.webp "Text Snippets in TX Text Control")

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

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Beyond WebSockets: A Glimpse into the Future of Document Editing with WebAssembly](https://www.textcontrol.com/blog/2026/06/10/beyond-websockets-glimpse-future-document-editing-webassembly/llms.txt)
- [5 Layout Patterns for Integrating the TX Text Control Document Editor in ASP.NET Core C#](https://www.textcontrol.com/blog/2026/04/09/5-layout-patterns-for-integrating-the-tx-text-control-document-editor-in-aspnet-core-csharp/llms.txt)
- [Introducing Text Control Agent Skills](https://www.textcontrol.com/blog/2026/03/27/introducing-text-control-agent-skills/llms.txt)
- [Deploying the TX Text Control Document Editor from the Private NuGet Feed to Azure App Services (Linux and Windows)](https://www.textcontrol.com/blog/2026/03/25/deploying-the-tx-text-control-document-editor-from-the-private-nuget-feed-to-azure-app-services-linux-and-windows/llms.txt)
- [Build a Custom Backstage View in ASP.NET Core with TX Text Control](https://www.textcontrol.com/blog/2026/02/17/build-a-custom-backstage-view-in-aspnet-core-with-tx-text-control/llms.txt)
- [ASP.NET Core Document Editor with Backend via the Text Control Private NuGet Feed](https://www.textcontrol.com/blog/2026/02/09/aspnet-core-document-editor-private-nuget-feed/llms.txt)
- [Why Document Processing Libraries Require a Document Editor](https://www.textcontrol.com/blog/2025/12/04/why-document-processing-libraries-require-a-document-editor/llms.txt)
- [Getting Started Video Tutorial: Document Editor in ASP.NET Core C# on Linux](https://www.textcontrol.com/blog/2025/07/29/getting-started-video-tutorial-document-editor-aspnet-core-csharp-linux/llms.txt)
- [Document Protection in ASP.NET with TX Text Control: Healthcare Use Cases](https://www.textcontrol.com/blog/2025/06/25/document-protection-in-asp-dotnet-with-tx-text-control-healthcare-use-cases/llms.txt)
- [Deploying the TX Text Control Document Editor in an ASP.NET Core Web App to Azure App Services](https://www.textcontrol.com/blog/2025/03/26/deploying-the-tx-text-control-document-editor-in-an-asp-net-core-web-app-to-azure-app-services/llms.txt)
- [Building an ASP.NET Core Backend (Linux and Windows) for the Document Editor and Viewer](https://www.textcontrol.com/blog/2025/03/26/building-an-asp-net-core-backend-for-the-document-editor-and-viewer/llms.txt)
- [TX Text Control for Blazor: Mail Merge Integration Tutorial](https://www.textcontrol.com/blog/2025/03/25/tx-text-control-for-blazor-mail-merge-integration-tutorial/llms.txt)
- [TX Text Control Document Editor and Viewer for Blazor Released](https://www.textcontrol.com/blog/2025/03/25/tx-text-control-document-editor-and-viewer-for-blazor-released/llms.txt)
- [Getting Started: Document Editor for Blazor in ASP.NET Core](https://www.textcontrol.com/blog/2025/03/25/getting-started-document-editor-for-blazor-in-asp-net-core/llms.txt)
- [Introducing TXTextControl.Web.Server.Core: A Cross-Platform Backend for TX Text Control Document Editor](https://www.textcontrol.com/blog/2025/03/13/introducing-txtextcontrol-web-server-core-a-cross-platform-backend-for-tx-text-control-document-editor/llms.txt)
- [Getting Started: Document Editor with ASP.NET Core and Docker Support with Linux Containers](https://www.textcontrol.com/blog/2025/03/12/getting-started-document-editor-with-asp-net-core-and-docker-support-with-linux-containers/llms.txt)
- [Announcing Our Work on a Blazor Component for Document Editing and Viewing](https://www.textcontrol.com/blog/2025/01/24/announcing-our-work-on-a-blazor-component-for-document-editing-and-viewing/llms.txt)
- [Preparing Documents for E-Signing for Multiple Signers in .NET C#](https://www.textcontrol.com/blog/2024/11/13/preparing-documents-for-e-signing-for-multiple-signers-in-net-c-sharp/llms.txt)
- [ASP.NET Core: Use the Document Editor and Viewer in the Same Razor View](https://www.textcontrol.com/blog/2024/11/08/asp-net-core-use-the-document-editor-and-viewer-in-the-same-razor-view/llms.txt)
- [Connecting the TXWebSocketMiddleware to a Separate, External TCP Synchronization Service](https://www.textcontrol.com/blog/2024/10/01/connecting-the-txwebsocketmiddleware-to-a-separate-external-tcp-synchronization-service/llms.txt)
- [Getting Started: Creating an ASP.NET Core Web App with the Document Editor in Visual Studio Code (VS Code)](https://www.textcontrol.com/blog/2024/09/27/getting-started-creating-an-asp-net-core-web-app-with-the-document-editor-in-visual-studio-code-vs-code/llms.txt)
- [Getting Started Video Tutorial: How to use the Document Editor in ASP.NET Core C#](https://www.textcontrol.com/blog/2024/08/05/getting-started-video-tutorial-how-to-use-the-document-editor-in-asp-net-core-csharp/llms.txt)
- [Observe When the Reporting Preview Tab is Active Using MutationObserver](https://www.textcontrol.com/blog/2024/07/23/observe-when-the-reporting-preview-tab-is-active-using-mutationobserver/llms.txt)
- [Removing Empty Pages in TX Text Control with JavaScript](https://www.textcontrol.com/blog/2024/06/19/removing-empty-pages-in-tx-textcontrol-with-javascript/llms.txt)
- [ASP.NET Core: Loading Documents in the Document Editor](https://www.textcontrol.com/blog/2024/04/15/asp-net-core-how-to-load-a-document-in-the-document-editor/llms.txt)
