Generating MS Word DOCX and PDF Documents with ASP.NET Core C#
This article shows how to create a simple ASP.NET Core application that generates MS Word DOCX and PDF documents using TX Text Control. It illustrates several ways to create documents from scratch and merge data into templates.

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.
-
In Visual Studio, create a new Console App using .NET 8.
-
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
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);
}
This code snippet initializes a document editing environment by using a Server
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.
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 Mail
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"
}
}
]
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);
}
The generated document contains the populated values from the JSON data, as shown in the screenshot below.
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);
}
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);
}
The resulting PDF document contains the HTML content with the added header, as shown in the screenshot below.
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 and start creating documents programmatically with C#.
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.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
PDF Conversion in .NET: Convert DOCX, HTML and more with C#
PDF conversion in .NET is a standard requirement for generating invoices, templates, and accessible reports. This article provides an overview of PDF conversion capabilities using TX Text Control,…
Convert MS Word DOCX to PDF including Text Reflow using .NET C# on Linux
This article explains how to use TX Text Control .NET Server to convert a Microsoft Word DOCX document to a PDF file on a Linux system using .NET C#. This conversion process includes text reflow,…
Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX…
This article explores how to use the TX Text Control MailMerge feature in .NET applications on Linux to generate pixel-perfect PDFs from DOCX templates. This powerful combination enables…
Sign Documents with a Self-Signed Digital ID From Adobe Acrobat Reader in…
This article shows how to create a self-signed digital ID using Adobe Acrobat Reader and how to use it to sign documents in .NET C#. The article also shows how to create a PDF document with a…
Converting MS Word DOCX Documents to PDF in C#
Use TX Text Control to programmatically convert MS Word DOC and DOCX documents to PDF. This article outlines the requirements and explains the simple steps you need to take to successfully convert…