How to Programmatically Convert Word DOCX to PDF Using C# .NET
What You Will Need
• Document Solutions for Word
• Visual Studio
• .NET 6+
Controls Referenced
• Document Solutions for Word (DsWord) — C# .NET Word API
• Documentation | Online Demo Explorer
Tutorial Concept
Learn how to convert Word DOCX files to PDFs using C#.
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.
Steps to Convert Word DOCX Files to PDFs in a C# .NET Server-Side Application
- Create a Sample .NET Core Console Application
- Load the Word Document (.docx) File into a C# .NET Word API Using the Load Method
- Save the Word Instance as a PDF Using the PdfOutputSettings Method
Create a Sample C# .NET Core Console Application
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;
Load the Word Document (.docx) File into a C# .NET Word API
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");
Save the .NET Word Instance as a PDF
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:
- CompressionLevel: This specifies values that indicate whether a compression operation emphasizes speed or compression size. It requires the CompressionLevel Enum to set different values.
- PdfAConformanceLevel: It sets the PDF/A conformance levels with the help of the PdfAConformanceLevel Enum
- DocumentInfo: The DocumentInfo object contains information about the document, like author, title, creation date, etc.
- ImageOptions: It gets and sets the ImageOptions object, which contains options to control how images are processed in the current document.
- Linearized: It indicates whether the generated PDF should be saved as linearized.
- Metadata: It gets or sets the metadata associated with the document.
- PdfVersion: It gets or sets the PDF Version of the generated document.
By default, the version is determined automatically based on which features are used in this document. Setting this property to a non-null string in the format “1.X” (where X is a digit from 0 to 9) overrides the automatic value with the specified one. - BackColor: It sets the background color. The default is white.
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.
Learn More About This C# .NET Word API
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.
Originally published at https://developer.mescius.com on August 23, 2024.