TX Text Control .NET allows developers to create PDF files programmatically using C# in various ways including creating documents from scratch, merging data into pre-defined templates and by converting documents such as HTML to PDF.

Requirements

In order to create the project in this tutorial, it is required to install TX Text Control .NET Server for ASP.NET. Download a trial version here:

Download trial version

This article doesn't show how to create new projects using TX Text Control, but how to create the documents itself. To learn how to create a new project using TX Text Control, please have a look at this tutorial:

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

Creating PDFs from Scratch

TX Text Control can be used to create all supported document types with the same streamlined API TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
TXTextControl Namespace
. After creating a new instance of 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.
, content such as text, tables and images can be added programmatically. The following code shows how to create a new instance of ServerTextControl in order to insert text and a table. At the end, the document is exported as an Adobe PDF document.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
// insert a table
tx.Tables.Add(3, 3, 10);
foreach (TXTextControl.TableCell tableCell in tx.Tables.GetItem(10).Cells) {
tableCell.Text = "Row " +
tableCell.Row.ToString() +
", Col " + tableCell.Column.ToString();
tableCell.CellFormat.BottomBorderWidth = 10;
}
// create a selection object
TXTextControl.Selection selection = new TXTextControl.Selection() {
FontSize = 500,
Text = "Welcome to Text Control",
Bold = true,
ForeColor = System.Drawing.Color.DarkRed
};
// apply the selection
tx.Selection = selection;
// save the document
tx.Save("results.pdf", TXTextControl.StreamType.AdobePDF);
}
view raw test.cs hosted with ❤ by GitHub

Creating documents with TX Text Control

Creating PDFs from MS Word Templates

Another method to create PDF documents is to use MS Word templates such as Office Open XML (*.docx) files in order to merge data into merge fields. 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 is merging JSON data into MS Word compatible merge fields in order 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

Creating documents with TX Text Control

Learn More

TX Text Control's MailMerge engine allows you to perform MS Word compatible mail merge processes in .NET based applications. This article gives an overview of the basic functionality and shows how to merge JSON data into templates.

Mail Merge MS Word Office Open XML (DOCX) Templates in C#

Converting Documents

Another way to create PDF documents is to convert them from other formats such as HTML. Using TX Text Control, you can load the content and create the Adobe PDF document by saving the content.

The following code shows how to load an HTML string into a new instance of ServerTextControl in order to export the document as an Adobe PDF file.

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

Creating documents with TX Text Control

Extended PDF Functionality

TX Text Control provides many extended PDF functionalities and can be used to create electronic invoices and digitally signed documents.

TX Text Control can be efficiently used to create Adobe PDF documents with digital signatures. These signatures can be created with PFX, DER Cer or Base64 CER certificate files. All you need is a valid certificate that is defined in the SaveSettings TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
SaveSettings Class
The SaveSettings class provides properties for advanced settings and information during save operations.
class.

TXTextControl.SaveSettings settings = new TXTextControl.SaveSettings();
X509Certificate2 cert = new X509Certificate2("test.pfx", "123");
settings.DigitalSignature = new TXTextControl.DigitalSignature(cert, null);
textControl1.Save("results.pdf", TXTextControl.StreamType.AdobePDF, settings);
view raw data.cs hosted with ❤ by GitHub

Learn More

TX Text Control can be used to create PDF forms, PDF files with embedded attachments or to digitally sign documents. Learn more about these feature in these articles:

Creating Adobe PDF Forms in C#

Creating ZUGFeRD Compliant PDF Invoices in C#

Adding Electronic and Digital Signatures to Documents in C# and Angular