When working with Microsoft Word DOCX documents, it’s often necessary to save data in non-Word formats, like PDF, to preserve formatting and ensure document security. Document Solutions for Word (DsWord) offers a complete C#/VB .NET solution to programmatically convert Word documents to PDF, making it ideal for creating consistent and secure documents, such as invoices, newsletters, journals, project plans, and articles. With a feature-rich object model based on Microsoft Office API, Word JavaScript API, and OpenXML SDK, DsWord allows developers to easily create, modify, and convert DOCX files across various operating systems, including Windows, Mac, and Linux. In this demo, we'll cover how to use the C# .NET Word API to load a Word document (.docx) into a server-side instance and convert it to PDF using the PdfOutputSettings method. Download a finished sample app to follow along.
Open Visual Studio 2022 and create a new C# Console App. We named ours DsWord_Convert_Word_to_PDFConsole.
Right-click the project in the Solution Explorer and choose Manage NuGet Packages. In the Package source at the top right, select nuget.org. Click the Browse tab on the top left and search for "DS.Documents." On the left panel, select DS.Documents.Word. In the Preview Changes dialog, click OK and choose I Accept on the next screen.
Add the needed namespaces to the Program.cs file:
using GrapeCity.Documents.Word; using GrapeCity.Documents.Word.Layout; using System.IO.Compression;
Now, we can load the Word DOCX file into a server-side Word instance.
Add the following lines of code in the main function of Program.cs to create a new DsWord document object, and then load the Word document into the object using the Load method.
// Initalize Word instance var wordDoc = new GcWordDocument(); // Load DOCX file wordDoc.Load(@"ProcurementLetter.docx");
To save the loaded DOCX file as a PDF, we need to get the layout of the .NET Word instance using the GcWordLayout class. The API allows us to define the options for saving the layout in PDF format using the PdfOutputSettings class setting. Options include:
Lastly, invoke the document layout’s SaveAsPdf method to save the loaded DOCX file as a PDF.
// Create an instance of the GcWordLayout using (var layout = new GcWordLayout(wordDoc)) < // Define the PDF output settings PdfOutputSettings pdfOutputSettings = new PdfOutputSettings(); pdfOutputSettings.CompressionLevel = CompressionLevel.Fastest; pdfOutputSettings.ConformanceLevel = GrapeCity.Documents.Pdf.PdfAConformanceLevel.PdfA1a; // Save the Word layout as a PDF layout.SaveAsPdf("ProcurementLetter.pdf", null, pdfOutputSettings); >
Run the application and notice the PDF document will be generated in your project. And just like that, we have loaded the Word document and converted it to a PDF.
This article only scratches the surface of the full capabilities of Document Solutions for Word, our .NET Word API library. Check out our online demo explorer and documentation to learn more.