Print MS Word DOCX Documents in .NET using C# Without Interop
This article shows how to print MS Word DOCX documents in .NET using C#. It uses TX Text Control to load Office Open XML documents and print them to any printer, PDF or image file.

Although there are libraries such as the Open XML SDK for creating DOCX files, printing is a different story. The only way to print a DOCX file is to open it in Microsoft Word and print it manually. For a web application that needs to print a lot of documents, or for a server-side application that needs to print documents automatically, this is not a good solution.
TX Text Control provides not only the ability to programmatically create DOCX files, but also the ability to print them without using MS Word or Microsoft Office Interop. This article shows the different ways to print a DOCX file using TX Text Control:
- Direct printing with full printer access
- Creating a PDF file
- Creating SVG images from the document pages
- Creating images from the document pages
Preparing the Application
A .NET 8 console application is created for the purposes of this demo.
Prerequisites
The following tutorial requires a trial version of TX Text Control .NET Server.
-
In Visual Studio, create a new Console App using .NET 8.
-
In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.
Select Text Control Offline Packages from the Package source drop-down.
Install the latest versions of the following package:
- TXTextControl.TextControl.ASP.SDK
Direct Printing
TX Text Control provides a Print method that can be used to print the current document to the default printer. The following code shows how to print a document to the default printer in a Console application.
using System.Drawing.Printing;
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
tx.Load("test.docx", TXTextControl.StreamType.WordprocessingML);
PrintDocument printDocument = new PrintDocument();
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
tx.Print(printDocument);
}
TX Text Control allows you to load and print not only DOCX files, but also binary MS Word format DOC and RTF files.
You can also print to a specific printer by passing the printer name to the PrinterSettings object. In this case the printer name is Microsoft Print to PDF and the output is a PDF file.
using System.Drawing.Printing;
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
tx.Load("test.docx", TXTextControl.StreamType.WordprocessingML);
PrintDocument printDocument = new PrintDocument();
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.PrinterSettings.PrintToFile = true;
printDocument.PrinterSettings.PrintFileName = "test.pdf";
tx.Print(printDocument);
}
Creating a PDF File
TX Text Control provides the Save method that can be used to save the current document to a PDF file. The following code shows how to save a document to a PDF file in a Console application.
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
tx.Load("test.docx", TXTextControl.StreamType.WordprocessingML);
tx.Save("test.pdf", TXTextControl.StreamType.AdobePDF);
}
This code uses the ServerTextControl to directly generate the binary PDF document without using a printer driver. The PDF file is stored in the file system. The Save method can be used to save the document in various formats including DOCX, DOC, RTF, and PDF.
Creating SVG Images
TX Text Control can also be used to create SVG images from the document pages. The advantage of SVG is that it's a standard that's supported by all browsers, and because it's a vector graphics format, there's no loss of quality for text content.
The Get
string svgSources = tx.GetPages()[1].GetImage(TXTextControl.Page.PageContent.All, 300);
The first parameter PageContent defines the content to be returned by the method. The second parameter defines the resolution of embedded bitmap images.
The following method creates SVG images from all pages of a document and saves them to the file system.
public string[] CreateSVG(ServerTextControl TextControl,
bool GlyphOutlines = false,
int FromPage = 1,
int ToPage = -1)
{
// create array for SVGs
string[] svgPages = new string[(ToPage == -1 ? TextControl.Pages : ToPage)];
// set page content
TXTextControl.Page.PageContent pageContent =
GlyphOutlines ? TXTextControl.Page.PageContent.All | TXTextControl.Page.PageContent.GlyphOutlines
: TXTextControl.Page.PageContent.All;
for (int i = FromPage; i <= (ToPage == -1 ? TextControl.Pages : ToPage); i++)
{
// get SVG from page
svgPages[i - 1] = TextControl.GetPages()[i].GetImage(pageContent, 96);
}
return svgPages;
}
Creating Images
TX Text Control can also be used to create images such as JPG or PNG from the document pages. The following code shows how to create images from the document pages in a Console application.
using System.Collections;
using System.Drawing.Imaging;
using System.Drawing;
using TXTextControl;
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
tx.Load("test.docx", TXTextControl.StreamType.WordprocessingML);
ArrayList inputImages = new ArrayList();
foreach (Page page in tx.GetPages())
{
MemoryStream image = new MemoryStream();
Bitmap mf = page.GetImage(100, TXTextControl.Page.PageContent.All);
mf.Save(image, ImageFormat.Png);
inputImages.Add(image);
}
// save images as files
int i = 0;
foreach (MemoryStream ms in inputImages)
{
FileStream file = new FileStream("image" + i.ToString() + ".png", FileMode.Create, FileAccess.Write);
ms.WriteTo(file);
file.Close();
i++;
}
}
The GetImage method has an implementation that returns a Bitmap object of the page which are stored in an array of Bitmap objects. The second part shows how to save these images to the file system.
Conclusion
TX Text Control provides the ability to programmatically create DOCX files, and also the ability to print them without using MS Word or Microsoft Office Interop. This article showed the various ways to print a DOCX file using TX Text Control.
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.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format
Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…
Expert Implementation Services for Legacy System Modernization
We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.
Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5
TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…