The TX Text Control document processing library provides a powerful and comprehensive API for programmatically creating, reading, converting, and editing MS Word documents in .NET applications. It is typically used to create MS Word and PDF documents by merging data into templates or by creating these documents from scratch.

This article is an introduction to the most common ways to create documents programmatically with C#.

Preparing the Application

A .NET 6 console application is created for the purposes of this demo.

Prerequisites

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

  1. In Visual Studio, create a new Console App using .NET 8.

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

    Create PDF

Creating Documents from Scratch

TX Text Control provides a powerful and complete API for programmatically creating and manipulating documents. This API can be used in any .NET application such as ASP.NET Core, Windows or WPF. For demonstration purposes, all of the code snippets have been written for console applications.

The following code snippet shows how to create a new document from scratch:

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);
}
view raw test.cs hosted with ❤ by GitHub

This code snippet initializes a document editing environment by using a ServerTextControl TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
ServerTextControl Class
The ServerTextControl class implements a component that provide high-level text processing features for server-based applications.
instance, adds formatted text and tables to the document, populates table cells with content, and then saves the document in both DOCX and PDF formats. It effectively demonstrates the basic capabilities of manipulating and creating documents using the ServerTextControl class.

Learn More

In this tutorial, you will learn how to create a new ASP.NET Core Web application that generates PDF documents and how to make those documents available by using the Document Viewer.

Create or Generate PDF Files in ASP.NET Core with C#

Creating Documents from Templates

Using TX Text Control, you can create templates with placeholder text (merge fields) and repeating sections (merge blocks) in MS Word or the full-featured document editor that comes with TX Text Control. During the merge process, given hierarchical data such as JSON is merged into merge fields and merge blocks to generate the resulting document.

The following simple template is used for the merge process using the MailMerge TX Text Control .NET Server for ASP.NET
DocumentServer Namespace
MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services.
class.

Creating documents with TX Text Control

The following JSON data is used as a simplified data source. TX Text Control supports complex hierarchical data with nested merge block structures.

[
{
"company_name": "Text Control, LLC",
"address":
{
"street": "1111 Text Control Way",
"zip": "28226",
"city": "Charlotte",
"country": "United States"
}
}
]
view raw data.json hosted with ❤ by GitHub

The following C# code merges JSON data into MS Word compatible merge fields to create the resulting PDF document.

TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings() {
ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord };
string jsonData = System.IO.File.ReadAllText("data.json");
using (TXTextControl.ServerTextControl serverTextControl =
new TXTextControl.ServerTextControl()) {
serverTextControl.Create();
serverTextControl.Load("template.docx", TXTextControl.StreamType.WordprocessingML, ls);
using (MailMerge mailMerge = new MailMerge()) {
mailMerge.TextComponent = serverTextControl;
mailMerge.MergeJsonData(jsonData);
}
// export document as PDF
serverTextControl.Save("results.pdf", TXTextControl.StreamType.AdobePDF);
}
view raw test.cs hosted with ❤ by GitHub

The generated document contains the populated values from the JSON data, as shown in the screenshot below.

Creating documents with TX Text Control

Creating Documents by Converting

TX Text Control provides a powerful API to convert documents between different formats. A typical process would be to generate an MS Word or PDF document from some other format, such as HTML. The following code snippet demonstrates how to convert an HTML string to a PDF document.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
var sHtml = "<strong>This is a <span style=\"color: red;\">sample</span> <u>document</u>!</strong>";
tx.Load(sHtml, TXTextControl.StringStreamType.HTMLFormat);
// save the document
tx.Save("results.pdf", TXTextControl.StreamType.AdobePDF);
}
view raw test.cs hosted with ❤ by GitHub

Once a document is loaded into the ServerTextControl, not only can it be converted to any other supported format, but it can also be manipulated using the full-featured API.

The following code demonstrates loading the existing HTML and adding a header to the document before exporting to PDF.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
var sHtml = "<strong>This is a <span style=\"color: red;\">sample</span> <u>document</u>!</strong>";
tx.Load(sHtml, TXTextControl.StringStreamType.HTMLFormat);
// adding a header
tx.HeadersAndFooters.Add(TXTextControl.HeaderFooterType.Header);
var header = tx.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Header);
header.Selection.Text = "This is the header";
// save the document
tx.Save("results.pdf", TXTextControl.StreamType.AdobePDF);
}
view raw test.cs hosted with ❤ by GitHub

The resulting PDF document contains the HTML content with the added header, as shown in the screenshot below.

Creating documents with TX Text Control

Conclusion

This article illustrated several ways to create documents programmatically using TX Text Control in .NET applications. It showed how to create documents from scratch, how to merge data into templates, and how to convert documents between different formats.

TX Text Control provides a powerful and comprehensive API for creating, reading, converting, and editing MS Word documents in .NET applications. It is typically used to create MS Word and PDF documents by merging data into templates or by creating these documents from scratch.

Download a free trial version of TX Text Control .NET Server for ASP.NET and start creating documents programmatically with C#.

Download Trial Version