Products Technologies Demo Docs Blog Support Company

How to Create PDF Documents with TX Text Control using C# .NET on Linux

This article shows how to create professional PDF and PDF/A documents using ASP.NET Core C# on Linux. 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.

How to Create PDF Documents with TX Text Control using C# .NET on Linux

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:

Creating the Application

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 8 SDK.

  1. In Visual Studio 2022, create a new project by choosing Create a new project.

  2. Select Console App as the project template and confirm with Next.

  3. Enter a project name and choose a location to save the project. Confirm with Next.

  4. Choose .NET 8.0 (Long Term Support) as the Framework.

  5. Enable the Enable container support checkbox and choose Linux as the Container OS.

  6. Choose Dockerfile for the Container build type option and confirm with Create.

    Creating the .NET 8 project

Adding the NuGet Packages

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

    ASP.NET Core Web Application

Using ServerTextControl and MailMerge

  1. Find the Program.cs file in the Solution Explorer and replace the code with the following code snippet:

    using 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);
    }
  2. 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.

Created document with TX Text Control

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.

When viewed in Adobe Acrobat Reader, the PDF document looks like this:

Created document with a table

Adding Headers and Footers

You can add headers and footers to a document using the HeadersAndFooters 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:

Created document with a header

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.

Created document with a header

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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

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.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETASP.NET CoreDOCX

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,…


ASP.NETASP.NET CoreFonts

TX Text Control Linux Preview: Font Handling

TX Text Control for Linux introduces a revamped, cross-platform font rendering system that eliminates the need for manual font installation. The new Linux version simplifies deployment by offering…


ASP.NETASP.NET CoreExtraction

Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas

Mining PDFs with Regex in C# can be a powerful technique for extracting information from documents. This article explores practical patterns, tips, and ideas for effectively using regular…


ASP.NETASP.NET CoreForms

Streamline Data Collection with Embedded Forms in C# .NET

Discover how to enhance your C# .NET applications by embedding forms for data collection. This article explores the benefits of using Text Control's ASP.NET and ASP.NET Core components to create…


ASP.NETASP.NET CorePDF

Adding QR Codes to PDF Documents in C# .NET

This article explains how to add QR codes to PDF documents with the Text Control .NET Server component in C#. It provides the necessary steps and code snippets for effectively implementing this…