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.
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 for ASP.NET:
- Download Trial Version
Setup download and installation required.
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
Converting a DOCX File to PDF
-
Find the Program.cs file in the Solution Explorer and replace the code with the following code snippet:
This file contains hidden or 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 (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); } -
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.