Products Technologies Demo Docs Blog Support Company

Edit MS Word DOCX Files in .NET C# and ASP.NET Core

This article shows how to edit MS Word DOCX files in .NET C# and ASP.NET Core using the TX Text Control Document Editor and also how to manipulate documents without a user interface using the ServerTextControl class.

Edit MS Word DOCX Files in .NET C# and ASP.NET Core

Developers need powerful, reliable, and versatile tools for working with Microsoft Word documents. For working with Word documents in DOCX, DOC and RTF formats, TX Text Control is the ideal solution. Whether you need a full-featured document editor or libraries to programmatically manipulate documents, TX Text Control has you covered.

Document Editor

TX Text Control provides an advanced document editor designed specifically for ASP.NET and ASP.NET Core applications. This editor allows users to:

  • Seamlessly edit Word documents: With the same ease as Microsoft Word, open, edit, and save DOCX, DOC, and RTF documents.
  • Utilize rich text features: Add and edit tables, images, headers, footers, and other elements with precision.
  • Working with forms and fields: Create fillable forms with text fields, checkboxes, and drop-down menus for interactive user experiences.

Headless Document Processing

Sometimes you need to programmatically modify or create Word documents without a user interface. TX Text Control's libraries make this possible:

  • Create documents from scratch: Create fully customizable documents, such as invoices, reports, or letters.
  • Modify existing documents: Insert, edit, or delete content programmatically for automation and efficiency.
  • Merge data into templates: Populate Word templates with dynamic data to create personalized documents.
  • Automate workflows: Integrate document editing into back-end workflows for seamless document processing.

Getting Started: Editor

Follow these steps to create an ASP.NET Core application that uses the TX Text Control Document Editor.

Prerequisites

Before you start, make sure you have downloaded the trial version of TX Text Control.

Creating the Application

Make sure that you have downloaded the latest version of Visual Studio 2022, which is included with the .NET 8 SDK.

  1. In Visual Studio 2022, create a new project by choosing Create a new project.

  2. Select ASP.NET Core Web App (Model-View-Controller) as the project template and confirm with Next.

  3. Choose a name for your project and confirm with Next.

  4. In the next dialog, choose .NET 8 (Long Term Support) as the Framework and confirm with Create.

    Creating the .NET 6 project

Adding the NuGet Package

  1. In the Solution Explorer, select the project you created and choose Manage NuGet Packages... from the Project main menu.

    Package Source

    Select either Text Control Offline Packages or nuget.org as the Package source. Packages in the official Text Control NuGet profile are updated frequently.

    Browse for txtextcontrol.web and Install the latest version of the TXTextControl.Web package.

    ASP.NET Core Web Application

Configure the Application

  1. Open the Program.cs file located in the project's root folder.

    At the very top of the file, insert the following code:

    using TXTextControl.Web;

    Add the following code after the entry app.UseStaticFiles();:

    // enable Web Sockets
    app.UseWebSockets();
    
    // attach the Text Control WebSocketHandler middleware
    app.UseTXWebSocketMiddleware();

Adding the Control to the View

  1. Find the Index.cshtml file in the Views -> Home folder. Replace the complete content with the following code to add the document editor to the view:

    @using TXTextControl.Web.MVC
        
    @{
        var sDocument = "<html><body><p>Welcome to <strong>Text Control</strong></p></body></html>";
    }
    
    @Html.TXTextControl().TextControl(settings => {
        settings.UserNames = new string[] { "Tim Typer" };
    }).LoadText(sDocument, TXTextControl.Web.StringStreamType.HTMLFormat).Render()
    
    <input type="button" onclick="insertTable()" value="Insert Table" />
    
    <script>
        function insertTable() {
            TXTextControl.tables.add(5, 5, 10, function(e) {
              if (e === true) { // if added
                TXTextControl.tables.getItem(function(table) {
                  table.cells.forEach(function(cell) {
    
                    cell.setText("Cell text");
    
                  });
                }, null, 10);
              }
            })
        }
    </script>

Run the Application

  1. Run the application by pressing F5 or by choosing Debug -> Start Debugging from the main menu. You get a full-featured document editor in your ASP.NET Core application.

    Running the application

Non-UI Document Processing

TX Text Control provides a powerful API for programmatically creating, modifying, and manipulating documents. The following tutorial shows how to create a simple .NET console application that uses the TX Text Control to create a document from scratch.

Prerequisites

The following tutorial requires a trial version of TX Text Control .NET Server.

  1. In Visual Studio 2022, create a new project by choosing Create a new project.

  2. Select Console App as the project template and confirm with Next.

  3. Choose a name for your project and confirm with Next.

  4. In the next dialog, choose .NET 8 (Long-term support) as the Framework and confirm with Create.

Adding the NuGet Package

  1. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

    Select Text Control Offline Packages from the Package source drop-down.

    Install the latest versions of the following package:

    • TXTextControl.TextControl.ASP.SDK

    ASP.NET Core Web Application

Create Documents

  1. Open the Program.cs file located in the project root folder and include this code:

    using TXTextControl;
    
    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();
    
        // Add a paragraph to the document
        TXTextControl.Selection selection = new TXTextControl.Selection(0, 0);
        selection.Text = "Hello Office Open XML!";
    
        // Set the paragraph format
        selection.ParagraphFormat = new ParagraphFormat()
        {
            Alignment = HorizontalAlignment.Center,
            BackColor = System.Drawing.Color.LightGray,
            Frame = Frame.BottomLine,
            FrameLineColor = System.Drawing.Color.Red,
            FrameStyle = FrameStyle.Double,
            FrameLineWidth = 10
        };
    
        // Set the font format
        selection.FontSize = 240; // 240 twips = 12 pt
        selection.FontName = "Arial";
        selection.Bold = true;
    
        // Apply the changes to the document
        tx.Selection = selection;
    
        // Save the document
        tx.Save("results.docx", TXTextControl.StreamType.WordprocessingML);
    }

This creates a new document and adds a paragraph with specific formatting, including borders, alignment, and character style. The document is then saved in DOCX format.

MS Word Document

Learn More

This article provides a guide on using TX Text Control to create MS Word DOCX including headers, footers, and tables.

Create Word Document with .NET C#

Conclusion

TX Text Control provides robust and versatile tools for seamlessly handling Word documents in ASP.NET and ASP.NET Core applications. Whether your project requires a feature-rich, interactive document editor for users to create and modify content in real time, or powerful libraries for back-end, non-UI document processing tasks such as programmatically creating, editing, or converting documents, TX Text Control provides comprehensive solutions. By combining ease of use with advanced functionality, TX Text Control ensures that developers have everything they need to implement sophisticated document management features in their applications.

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.NETASP.NET CoreDOCX

How to Import and Read Form Fields from DOCX Documents in .NET on Linux

Learn how to import and read form fields from DOCX documents in .NET on Linux using TX Text Control. This article provides a step-by-step guide to help you get started with form fields in TX Text…


ASP.NETASP.NET CoreDOCX

How to Programmatically Create MS Word DOCX Documents with .NET C# on Linux

Learn how to programmatically create MS Word DOCX documents using .NET C# on Linux with TX Text Control. This tutorial covers the necessary steps to set up your environment and create a simple…


ASP.NETASP.NET CoreDOC

Create Word Document with .NET C#

This article provides a guide on using TX Text Control to create MS Word DOCX and DOC documents in .NET applications, such as ASP.NET Core and Windows applications. It covers steps to set up a…


ASP.NETArchivingDOCX

From DOCX to Long-Term Archiving: Generating PDF/A from Office Documents in…

This article explores how to convert DOCX files to the PDF/A format using the .NET C# programming language. We discuss why PDF/A is important for long-term archiving and provide a step-by-step…


ASP.NETASP.NET CoreDOCX

Why HTML is not a Substitute for Page-Oriented Formats like DOCX

In this blog post, we will discuss the limitations of HTML as a document format and explain why page-oriented formats, such as DOCX, remain essential for certain use cases. We will explore the…