Programmatic PDF generation is a common requirement in modern applications, whether for invoices, reports, or dynamically generated documents. While .NET developers have several options for PDF creation, achieving high-quality, feature-rich documents on Linux can be challenging.
In this tutorial, we'll explore how to use TX Text Control to create, modify, and export PDF documents in a C#.NET application running on Linux. We'll walk through the setup, cover the key APIs, and demonstrate how to create PDFs with text formatting, images, and tables - all while leveraging the power of TX Text Control's document processing capabilities.
You will have a fully functional .NET-based PDF creation solution running seamlessly on Linux by the end of this guide.
Prerequisites
You need to download and install the trial version of TX Text Control .NET Server for ASP.NET:
- Download Trial Version
Setup download and installation required.
Creating the Application
Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 8 SDK.
-
In Visual Studio 2022, create a new project by choosing Create a new project.
-
Select Console App as the project template and confirm with Next.
-
Enter a project name and choose a location to save the project. Confirm with Next.
-
Choose .NET 8.0 (Long Term Support) as the Framework.
-
Enable the Enable container support checkbox and choose Linux as the Container OS.
-
Choose Dockerfile for the Container build type option and confirm with Create.
Adding the NuGet Packages
-
In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu. Select Text Control Offline Packages as the Package source.
Install the following package:
- TXTextControl.TextControl.Core.SDK
Using ServerTextControl and MailMerge
-
Find the Program.cs file in the Solution Explorer and replace the code with the following code snippet:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersusing TXTextControl; using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { tx.Create(); // Add a paragraph to the document TXTextControl.Selection selection = new TXTextControl.Selection(0, 0); selection.Text = "Hello, Linux!"; // Set the paragraph format selection.ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Center, BackColor = System.Drawing.Color.LightGray, Frame = Frame.BottomLine, FrameLineColor = System.Drawing.Color.Red, FrameStyle = FrameStyle.Double, FrameLineWidth = 10, }; // Set the font format selection.FontSize = 240; // 240 twips = 12 pt selection.FontName = "Arial"; selection.Bold = true; // Apply the changes to the document tx.Selection = selection; // Save the document tx.Save("results.pdf", TXTextControl.StreamType.AdobePDF); } -
Now run the project that runs the application in a Docker container that uses the default Debian container with the .NET 8.0 runtime preinstalled.
It creates a new document, inserts a paragraph with a defined format-including borders, alignment, and character style-and then saves the document as a PDF file.
Adding Tables
Inserting tables into a document is as easy as adding text. The following code snippet demonstrates how to create a table and apply formatting, including cell borders and background colors.
using TXTextControl; | |
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) | |
{ | |
tx.Create(); | |
tx.Tables.Add(5,5,10); | |
// Set the table border style | |
Table table = tx.Tables.GetItem(10); | |
// Set the table border style | |
TableCellFormat cellFormat = new TableCellFormat() { | |
BottomBorderWidth = 1, | |
BottomBorderColor = System.Drawing.Color.Red, | |
TopBorderWidth = 1, | |
TopBorderColor = System.Drawing.Color.Red, | |
LeftBorderWidth = 1, | |
LeftBorderColor = System.Drawing.Color.Red, | |
RightBorderWidth = 1, | |
RightBorderColor = System.Drawing.Color.Red, | |
BottomTextDistance = 80, | |
TopTextDistance = 80, | |
LeftTextDistance = 120, | |
RightTextDistance = 120 | |
}; | |
// Loop through all cells and set the cell format | |
foreach (TableCell cell in table.Cells) | |
{ | |
cell.CellFormat = cellFormat; | |
cell.Text = $"Cell: {cell.Row}/{cell.Column}"; | |
} | |
// Highlight the first column and row | |
TableCellFormat highlightCellFormat = new TableCellFormat() | |
{ | |
BackColor = System.Drawing.Color.LightGray | |
}; | |
table.Columns.GetItem(1).CellFormat = highlightCellFormat; | |
table.Rows.GetItem(1).CellFormat = highlightCellFormat; | |
// Save the document | |
tx.Save("results.pdf", TXTextControl.StreamType.AdobePDF); | |
} |
When viewed in Adobe Acrobat Reader, the PDF document looks like this:
Adding Headers and Footers
You can add headers and footers to a document using the Headers ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ HeaderFooterCollection Class
An instance of the HeaderFooterCollection class contains the headers and footers in a Text Control document represented through objects of the type HeaderFooter. collection. The following code snippet inserts a header into the current section with an image in the right corner and page number fields.
using TXTextControl; | |
using (ServerTextControl tx = new ServerTextControl()) | |
{ | |
tx.Create(); | |
// Add a header | |
tx.Sections.GetItem().HeadersAndFooters.Add(HeaderFooterType.Header); | |
// Get the header/footer object | |
HeaderFooter headerFooter = tx.Sections.GetItem().HeadersAndFooters.GetItem(HeaderFooterType.Header); | |
// Add an image to the header | |
Image image = new Image("tx_logo.svg", 0); | |
headerFooter.Images.Add(image, HorizontalAlignment.Right, 1, ImageInsertionMode.AboveTheText); | |
// Add a text to the header | |
headerFooter.Selection.Text = "Header Text\r\n\r\n"; | |
// Add a page number field to the header | |
headerFooter.Selection.Text = "Page "; | |
headerFooter.PageNumberFields.Add(new PageNumberField()); | |
headerFooter.Selection.Text = " of "; | |
headerFooter.PageNumberFields.Add(new PageNumberField() { ShowNumberOfPages = true }); | |
// Add a new page | |
tx.Selection.Text = "\f"; | |
// Save the document | |
tx.Save("results.pdf", StreamType.AdobePDF); | |
} |
Here is the result when viewed in Adobe Acrobat Reader:
More PDF Options
TX Text Control provides advanced PDF settings, including encryption, compression, and PDF/A compliance. Developers can configure digital signatures and PDF/A compliance using the SaveSettings class. When opened in Acrobat Reader, the document displays a compliance notification and allows signature verification.
In addition, TX Text Control supports PDF/A-3 compliant documents, enabling embedded files. Developers can set UserPassword (to open the document) and MasterPassword (to manage access rights) to control permissions such as printing and form field creation.
Conclusion
TX Text Control provides a powerful document processing API for .NET developers to create, modify, and export PDF documents in Linux environments. By following this guide, you will have learned how to create a .NET application that generates high-quality PDFs with text formatting, images, tables, headers, footers, and advanced PDF settings.
TX Text Control enables the creation of pixel-perfect PDF documents on any Linux environment that supports .NET, including Debian, Ubuntu, and Azure App Services.