If you need to programmatically convert an MS Word document in RTF, DOC or DOCX formats to PDF, TX Text Control provides an easy-to-use library to accomplish this task. Using advanced document processing technology, you can use a highly reliable C# API to create PDFs from MS Word documents without using printer drivers.
The following tutorial shows how to create a simple .NET 6 C# Console App to convert MS Word documents to PDF that uses the Server
╰ TXTextControl Namespace
╰ ServerTextControl Class
The ServerTextControl class implements a component that provide high-level text processing features for server-based applications. class.
Creating the Application
Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 6 SDK.
Prerequisites
The following tutorial requires a trial version of TX Text Control .NET Server for ASP.NET.
-
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.
-
Choose a name for your project and confirm with Next.
-
In the next dialog, choose .NET 6 (Long-term support) as the Framework and confirm with Create.
Adding the NuGet Package
-
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
Adding Documents
-
In the Solution Explorer, select your created project and choose New Folder from the Project main menu and name it Documents.
-
Select the newly created folder and choose Add Existing Item... from the Project main menu. Select any MS Word document from your local machine and click the Add button to confirm.
-
Select the newly created file and set the Copy to Output Directory property to true.
Adding the Code
-
Open the Program.cs file and replace the code with the following code:
This file contains 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.ServerTextControl tx = new TXTextControl.ServerTextControl()) { tx.Create(); // load DOC or RTF file //tx.Load("mydocument.doc", TXTextControl.StreamType.MSWord); //tx.Load("mydocument.rtf", TXTextControl.StreamType.RichTextFormat); // load DOCX file tx.Load("Documents/mydocument.docx", TXTextControl.StreamType.WordprocessingML); // optionally set the save settings TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() { Author = "Text Control", CreationDate = DateTime.Now, CreatorApplication = "TX Text Control Sample Application", DocumentAccessPermissions = TXTextControl.DocumentAccessPermissions.AllowAll }; // save as PDF tx.Save("mydocument.pdf", TXTextControl.StreamType.AdobePDF, saveSettings); Console.WriteLine("Document has been converted!"); }
Compile and start the application. The converted PDF document will be saved in the root folder of your project.
Different Ways to Generate PDFs
TX Text Control .NET allows developers to programmatically create PDF files using C# in a variety of ways, including creating documents from scratch, merging data into predefined templates, and converting documents such as HTML to PDF.
Learn More
TX Text Control allows developers to create PDF files programmatically using C#. This article shows various ways to create Adobe PDF documents.