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 for ASP.NET.
-
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 ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ ServerTextControl Class
╰ Print Method
Prints a Text Control document or a part of a document. 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 ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ ServerTextControl Class
╰ Save Method
Saves the complete contents of a document with the specified format. 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
╰ TXTextControl Namespace
╰ Page Class
╰ GetImage Method
Gets an image of the page's contents. method has an implementation that returns the page as a Base64-encoding string in SVG format.
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.