# 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, which is essential for creating responsive PDF documents that adapt to different page sizes and layouts.

- **Author:** Bjoern Meyer
- **Published:** 2025-06-10
- **Modified:** 2025-11-16
- **Description:** 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, which is essential for creating responsive PDF documents that adapt to different page sizes and layouts.
- **6 min read** (1090 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - DOCX
  - PDF
  - Linux
- **Web URL:** https://www.textcontrol.com/blog/2025/06/10/convert-ms-word-docx-to-pdf-including-text-reflow-using-dotnet-csharp-on-linux/
- **LLMs URL:** https://www.textcontrol.com/blog/2025/06/10/convert-ms-word-docx-to-pdf-including-text-reflow-using-dotnet-csharp-on-linux/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2025/06/10/convert-ms-word-docx-to-pdf-including-text-reflow-using-dotnet-csharp-on-linux/llms-full.txt

---

Although the DOCX format (Microsoft Word Office Open XML) is great for creating and editing templates, the PDF format (Portable Document Format) is preferred for final delivery. PDFs preserve the exact layout, fonts, and styles of a document, regardless of the device or platform. This is important for developers building systems that generate contracts, certificates, or branded documents, where visual fidelity is essential. The PDF format is standardized to ensure consistent presentation and is widely accepted across industries, including legal, finance, healthcare, and education.

#### Why Convert DOCX to PDF?

Additionally, PDFs are more secure to distribute. Unlike editable DOCX files, PDFs can be locked, digitally signed, and encrypted. This is important for systems that deliver official documents, such as invoices, medical reports, and compliance certificates, because tamper-proof delivery is critical for these documents. Password protection and digital signatures help ensure authenticity and prevent unauthorized modifications.

With TX Text Control, you can use Microsoft Word documents as templates to merge hierarchical data, such as JSON or XML, into placeholders and repeating merge blocks to generate final documents. These documents, as well as other Word documents, can be converted to PDF for deployment, printing, and archiving. This makes TX Text Control an ideal choice for automated document generation pipelines, enabling developers to generate pixel-perfect PDFs directly from structured data without manual intervention.

#### Reflow and Adaptation

TX Text Control allows you to rearrange text and content while applying a different page format or adding elements like headers and footers. Other pure converters, for example, use MS Word or their own engine to "print" the document to PDF. This process does not allow you to change or reflow the content. With TX Text Control, however, you get access to a fully featured document engine that adapts to new page sizes, different margins, added content, and text flow. This flexibility is useful for creating personalized or dynamic documents that vary in length and structure, such as quotations, reports, and dynamic letters.

Furthermore, TX Text Control's cross-platform capability, which includes Windows, Linux, and containerized environments, allows it to integrate seamlessly into modern cloud and microservice architectures. This ensures that the same high-fidelity document generation workflow can be consistently deployed across diverse environments. Whether you're developing web applications, desktop software, or backend services, TX Text Control offers tools that efficiently and reliably streamline the DOCX-to-PDF conversion process.

#### Document Reflow

With TX Text Control's document reflow feature, you can adapt the layout of a document when converting it to PDF. This means you can adjust the page size, margins, and other layout properties without compromising the content's integrity. During the reflow process, text and images are intelligently adjusted to fit the new layout, ensuring the final PDF looks professional and is easy to read.

The following example shows how to load a DOCX file and change the page size. As you can see, the text automatically adapts to fit the new layout, and the page count changes, which is automatically reflected in the page number field in the footer.

![Document Reflow](https://s1-www.textcontrol.com/assets/dist/blog/2025/06/10/a/assets/document_reflow.gif "Document Reflow")

### Creating the Application

In this example, we will build a .NET 8 console application using TX Text Control to convert a DOCX file to a PDF file. After loading the document, the page size will be changed so that the text automatically adapts.

> ### Prerequisites
> 
> You need to download and install the trial version of TX Text Control .NET Server:
> 
> - [Download Trial Version](https://www.textcontrol.com/product/tx-text-control-dotnet-server/download/)  
>     Setup download and installation required.

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0).

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](https://s1-www.textcontrol.com/assets/dist/blog/2025/06/10/a/assets/visualstudio1.webp "Creating the .NET 8 project")

#### Adding the NuGet Packages

7. 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](https://s1-www.textcontrol.com/assets/dist/blog/2025/06/10/a/assets/visualstudio2.webp "ASP.NET Core Web Application")

#### Converting a DOCX File to PDF

8. Find the *Program.cs* file in the *Solution Explorer* and replace the code with the following code snippet:
    
    ```
    using TXTextControl;
    
    using (ServerTextControl tx = new ServerTextControl())
    {
        tx.Create();
    
        // Load the document from a file
        tx.Load("agreement.docx", TXTextControl.StreamType.WordprocessingML);
    
        foreach (Section section in tx.Sections)
        {
            // Set the page size for each section
            section.Format.PageSize = new TXTextControl.PageSize(600,600);
        }
    
        // Save the document as PDF
        tx.Save("result.pdf", StreamType.AdobePDF);
    }
    ```
9. Now run the project that runs the application in a Docker container, WSL, or locally on your Windows.

The page size is changed for all sections of the document so that it is the same across all pages. Then, the resulting document with the rearranged text and elements is exported to PDF.

#### Changing the Size of Tables

The only missing feature is the ability to automatically change the size of tables, which TX Text Control does not offer. However, this can easily be done using a helper class. The following code modifies all tables to fit the new page size.

```
using TXTextControl;

using (ServerTextControl tx = new ServerTextControl())
{
    tx.Create();

    // Load the document from a file
    tx.Load("agreement.docx", TXTextControl.StreamType.WordprocessingML);

    foreach (Section section in tx.Sections)
    {
        // Set the page size for each section
        section.Format.PageSize = new TXTextControl.PageSize(600,600);
    }

    foreach (IFormattedText formattedText in tx.TextParts)
    {
        foreach (Table table in formattedText.Tables)
        {
            // Adapt the size of the table to fit the page
            table.AdaptSize(tx);
        }
    }

    // Save the document as PDF
    tx.Save("result.pdf", StreamType.AdobePDF);
}
```

This is the required code for the *TableExtender* helper class.

```
using TXTextControl;

public static class TableExtender
{

    public static void AdaptSize(this Table table, ServerTextControl TextControl)
    {

        if (table == null)
            return;

        TextControl.PageUnit = MeasuringUnit.Twips;

        // calculate the max available width
        var maxWidth = TextControl.Sections.GetItem().Format.PageSize.Width -
              TextControl.Sections.GetItem().Format.PageMargins.Left -
              TextControl.Sections.GetItem().Format.PageMargins.Right;

        // loop through all cells
        for (int row = 1; row <= table.Rows.Count; row++)
        {

            // reset the calculated width
            var currentWidth = 0;

            // row by row
            for (int col = 1; col <= table.Columns.Count; col++)
            {
                // calculate the complete width of the row
                currentWidth += table.Cells[row, col].Width;
            }

            // calculate the missing gap
            var destinationWidth = currentWidth - maxWidth;

            // loop through all columns in current row
            for (int col = 1; col <= table.Columns.Count; col++)
            {

                // calculate the ratio percentage for each cell
                float percentage = (float)table.Cells[row, col].Width /
                       (float)currentWidth;

                // resize the actual cell by it's calculated ratio
                table.Cells[row, col].Width = (int)(table.Cells[row, col].Width -
                              (destinationWidth * percentage));
            }

        }
    }
}
```

#### Conclusion

In this example, you learned how to use TX Text Control to convert a DOCX file to a PDF in a .NET 8 console application. You also learned how to adjust the page size so the text adapts automatically to the new layout. This feature is useful for generating pixel-perfect PDFs from structured data, such as JSON or XML, without manual intervention.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX Templates](https://www.textcontrol.com/blog/2025/05/27/use-mailmerge-in-dotnet-on-linux-to-generate-pixel-perfect-pdfs-from-docx-templates/llms.txt)
- [How to Create PDF Documents with TX Text Control using C# .NET on Linux](https://www.textcontrol.com/blog/2025/03/18/how-to-create-pdf-documents-with-tx-text-control-using-c-sharp-net-on-linux/llms.txt)
- [TX Text Control Linux Preview: Font Handling](https://www.textcontrol.com/blog/2024/12/28/tx-text-control-linux-preview-font-handling/llms.txt)
- [Sign Documents with a Self-Signed Digital ID From Adobe Acrobat Reader in .NET C#](https://www.textcontrol.com/blog/2024/08/12/sign-documents-with-a-self-signed-digital-id-from-adobe-acrobat-reader-in-net-c-sharp/llms.txt)
- [C# Document Generation: A Developer's Guide for .NET](https://www.textcontrol.com/blog/2026/07/08/csharp-document-generation-developer-guide-for-dotnet/llms.txt)
- [Validating PDF/UA Documents in .NET C#: A Practical Guide](https://www.textcontrol.com/blog/2026/07/06/validating-pdf-ua-documents-in-dotnet-csharp/llms.txt)
- [Using QR Codes in PDF Documents in C# .NET](https://www.textcontrol.com/blog/2026/04/21/using-qr-codes-in-pdf-documents-in-csharp-dotnet/llms.txt)
- [Programmatically Fill, Flatten, and Export DOCX Form Templates to PDF in C# .NET](https://www.textcontrol.com/blog/2026/04/10/programmatically-fill-flatten-and-export-docx-form-templates-to-pdf-in-csharp-dotnet/llms.txt)
- [Why Structured E-Invoices Still Need Tamper Protection using C# and .NET](https://www.textcontrol.com/blog/2026/03/24/why-structured-e-invoices-still-need-tamper-protection-using-csharp-and-dotnet/llms.txt)
- [Create Fillable PDFs from HTML Forms in C# ASP.NET Core Using a WYSIWYG Template](https://www.textcontrol.com/blog/2026/03/17/create-fillable-pdfs-from-html-forms-in-csharp-aspnet-core-using-a-wysiwyg-template/llms.txt)
- [Why HTML to PDF Conversion is Often the Wrong Choice for Business Documents in C# .NET](https://www.textcontrol.com/blog/2026/03/13/why-html-to-pdf-conversion-is-often-the-wrong-choice-for-business-documents-in-csharp-dot-net/llms.txt)
- [Document Editor with TX Spell .NET on Linux using ASP.NET Core](https://www.textcontrol.com/blog/2026/01/28/document-editor-tx-spell-net-linux-aspnet-core/llms.txt)
- [A Complete Guide to Converting Markdown to PDF in .NET C#](https://www.textcontrol.com/blog/2026/01/07/a-complete-guide-to-converting-markdown-to-pdf-in-dotnet-csharp/llms.txt)
- [Why PDF Creation Belongs at the End of the Business Process](https://www.textcontrol.com/blog/2026/01/02/why-pdf-creation-belongs-at-the-end-of-the-business-process/llms.txt)
- [How to Extend the Default Style Mapping when Converting DOCX to Markdown in .NET C#](https://www.textcontrol.com/blog/2025/12/22/how-to-extend-the-default-style-mapping-when-converting-docx-to-markdown-in-dotnet-csharp/llms.txt)
- [Designing the Perfect PDF Form with TX Text Control in .NET C#](https://www.textcontrol.com/blog/2025/12/16/designing-the-perfect-pdf-form-with-tx-text-control-in-dotnet-csharp/llms.txt)
- [Why Defining MIME Types for PDF/A Attachments Is Essential](https://www.textcontrol.com/blog/2025/12/10/why-defining-mime-types-for-pdfa-attachments-is-essential/llms.txt)
- [Validate Digital Signatures and the Integrity of PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/11/14/validate-digital-signatures-and-the-integrity-of-pdf-documents-in-csharp-dotnet/llms.txt)
- [Validate PDF/UA Documents and Verify Electronic Signatures in C# .NET](https://www.textcontrol.com/blog/2025/11/13/validate-pdf-ua-documents-and-verify-electronic-signatures-in-csharp-dotnet/llms.txt)
- [How To Choose the Right C# PDF Generation Library: Developer Checklist](https://www.textcontrol.com/blog/2025/11/12/how-to-choose-the-right-csharp-pdf-generation-library-developer-checklist/llms.txt)
- [Why Digitally Signing your PDFs is the Only Reliable Way to Prevent Tampering](https://www.textcontrol.com/blog/2025/10/30/why-digitally-signing-your-pdfs-is-the-only-reliable-way-to-prevent-tampering/llms.txt)
- [Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX Text Control and LLMs](https://www.textcontrol.com/blog/2025/10/16/automating-pdf-ua-accessibility-with-ai-describing-docx-documents-using-tx-text-control-and-llms/llms.txt)
- [Converting Office Open XML (DOCX) to PDF in Java](https://www.textcontrol.com/blog/2025/10/14/converting-office-open-xml-docx-to-pdf-in-java/llms.txt)
- [Extending DS Server with Custom Digital Signature APIs](https://www.textcontrol.com/blog/2025/10/09/extending-ds-server-with-custom-digital-signature-apis/llms.txt)
- [Why PDF/UA and PDF/A-3a Matter: Accessibility, Archiving, and Legal Compliance](https://www.textcontrol.com/blog/2025/10/07/why-pdf-ua-and-pdf-a-3a-matter-accessibility-archiving-and-legal-compliance/llms.txt)
