Creating PDF Files using TX Text Control .NET in C#
TX Text Control allows developers to create PDF files programmatically using C#. This article shows various ways to create Adobe PDF documents.

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. Download a trial version here:
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. After creating a new instance of Server
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);
}
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 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 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);
}
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.
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);
}
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 Save
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);
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
Related Posts
Extract Text and Data from PDF Documents in C#
TX Text Control can be used to create and edit Adobe PDF documents programmatically. But it is also possible to import PDF documents to read, extract and manipulate them. This article shows…
ASP.NETJavaScriptWindows Forms
Generating Interactive PDF Forms by Injecting JavaScript
Using TX Text Control, it is possible to export documents with form fields to fillable PDFs. This article shows how to inject JavaScript to add interaction to form fields.
Form Field Handling in PDF Documents
Since TX Text Control supports form fields, it is possible to either export form fields in the PDF document or to flatten the form fields to export text only without the field functionality. This…
Creating ZUGFeRD Compliant PDF Invoices in C#
ZUGFeRD / Factur-X documents can be created and extracted using TX Text Control X19. This article shows how to create a valid ZUGFeRD compliant invoice PDF document from scratch.
X19 Sneak Peek: Processing AcroForm Fields in Adobe PDF Documents
TX Text Control X19 will introduce a new namespace that contains classes to process PDF documents. A new feature allows the extraction of AcroForm fields from existing Adobe PDF documents.