Products Technologies Demo Docs Blog Support Company

.NET Microsoft Word Document API: Creating DOCX Documents with C#

TX Text Control provides a fast, powerful and reliable way to create MS Word documents programmatically using C#. There are many ways to create documents using Text Control products. This article gives an overview of different approaches using our products.

.NET Microsoft Word Document API: Creating DOCX Documents with C#

Text Control products can be used to create documents programmatically from scratch, pre-designed templates can be used to populate merge fields using the MailMerge class and documents can be edited using true WYSIWYG rich text controls for ASP.NET Core, Angular, Windows Forms and WPF.

  • Create, edit, load and save MS Word documents (DOC, DOCX, RTF).
  • Fully-featured document API to automate documents.
  • Merge data from multiple data sources into templates with placeholders.
  • Deploy TX Text Control within your application.

Creating Documents From Scratch

TX Text Control provides a powerful and complete API to create and manipulate documents programmatically. This API can be used in any .NET application such as Windows applications, Windows Service applications or web services.

The following code creates a new instance of ServerTextControl which is the non-visual Text Control. The API is compatible to the visual controls such as the Windows Forms TXTextControl.TextControl class, the WPF TXTextControl.WPF.TextControl class or the online editor using JavaScript.

The following code inserts a paragraph, a table and exports the document as an MS Word DOCX and Adobe PDF document:

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

    // create a new selection range
    Selection range = new Selection();
    range.Bold = true;
    range.FontSize = 400;
    range.Text = "This is a new paragraph\r\n";

    // insert the range to the document
    tx.Selection = range;

    // add a table
    tx.Tables.Add(5, 5, 10);

    foreach (TableCell cell in tx.Tables.GetItem(10).Cells)
    {
        cell.Text = cell.Row.ToString() + ", " + cell.Column.ToString();
    }

    // save the document as DOCX
    tx.Save("test.docx", StreamType.WordProcessingML);
    
    // save the document as PDF
    tx.Save("test.pdf", StreamType.AdobePDF);
}

Merging Templates Using MailMerge

After a template has been successfully designed in one of the visual document editors of TX Text Control, the reporting engine MailMerge can be used to merge data into placeholders. For each data row of the master table in the data source, a document is created. Merge fields are populated with column values and repeating blocks are merged with data rows of related child tables in the data source.

The following code creates an instance of the reporting engine MailMerge to merge the template that is loaded into a ServerTextControl with an IEnumerable object. Finally, the document is exported as a PDF document:

// create a business object that is used
// as the data source
Invoice invoice = new Invoice() { Name = "Report" };

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

    LoadSettings ls = new LoadSettings() {
        ApplicationFieldFormat = ApplicationFieldFormat.MSWord
    };

    // load the created template
    tx.Load("template.docx", StreamType.WordprocessingML, ls);

    // create a new MailMerge engine
    using (MailMerge mailMerge = new MailMerge())
    {
        // connect to ServerTextControl
        mailMerge.TextComponent = tx;

        // merge the template that is loaded with
        // the business object
        mailMerge.MergeObject(invoice);                
    }

    // export the document as PDF
    tx.Save("test.pdf", StreamType.AdobePDF);
}

Visual Document Editor

The RibbonReportingTab represents a ribbon tab to integrate mail merge and reporting functionality directly into your TX Text Control based application. Together with all other ribbon tabs known from typical word processors, creating MS Word compatible template designers and word processors is a very easy task.

Not being dependent on an additional third-party tool such as MS Word to create templates is a very important key aspect when implementing word processing functionality into business applications. The MS Word compatible editor has the look and feel of MS Word, but can be customized and adapted to user requirements. MS Word templates can be reused and edited in TX Text Control.

The following screenshot shows the ASP.NET Core document editor of TX Text Control. The visual controls can be automated in the same way like the non-visual class ServerTextControl using the same, compatible API.

Creating documents with TX Text Control

Text Control provides the most powerful tools and controls to integrate document creation processes into any kind of business applications.

Open the online demos to learn how TX Text Control can help you integrating document processing into your applications:

Live Demos

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Angular

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

Learn more about Angular

Related Posts

AngularASP.NETConference

Impressions from Web Developer Conference WDC 2023 in Hamburg, Germany

This week we sponsored and exhibited at the Web Developer Conference in Hamburg, Germany. In this article you can see some pictures of our booth area and the conference.


AngularASP.NETConference

Impressions from NDC London 2023

Last week, we exhibited at NDC London 2023 in the center of London city and sponsored the attendee party. In this article, you will find some impressions of our booth area and the overall conference.


AngularASP.NETASP.NET Core

See Text Control at DEVintersection Fall 2022 in Las Vegas

In December, DEVintersection is coming back to the MGM Grand in Las Vegas. We will be exhibiting at this conference to show our digital document processing technology.


AngularASP.NETConference

Impressions from NDC Oslo 2022

Last week, we exhibited at NDC Oslo 2022 at the Oslo Spektrum in the center of the city. More than 2500 attendees met to learn from international speakers from around the world. See some…


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…