Skip to main content Skip to footer

How to Programmatically Convert Word DOCX to PDF Using C# .NET

When working with Word documents in .NET Standard 2.0 applications, there are times when it is necessary to store data in non-Word formats. One of the most popular formats is PDF because of its ability to preserve formatting and the security of the document itself. Programmatically saving a file through DsWord C#/VB .NET API for PDF is the ideal way to accomplish this.

Why Convert a Word Document (.docx) to PDF?

  • You want the format of your documents to be consistent. A PDF will keep all original formatting, regardless of the operating system or computer used to open the file
  • You need long-term data preservation. Working with PDF formats will allow you to share, collaborate, and ensure the security of the content within your documents, long-term
  • You may have designed Word documents from other data sources and need to create documents such as:

    • Invoice slips
    • Newsletters
    • Journals
    • Project Plans
    • Articles

Document Solutions for Word (DsWord, previously GcWord) is a Word API offering a complete C#/VB .NET solution to program and work with Word documents, with zero dependencies on Microsoft Office.

With the DsWord API, developers create powerful document conversions for archival and delivery systems in .NET Standard 2.0 targeted applications, all with the ability to programmatically create these conversions on various operating systems, including Windows, Mac, and Linux.

DsWord's feature-rich object model is based on Microsoft Office API, Word JavaScript API, and OpenXML SDK. The architecture is simple and easy to use, so creating DOCX files through C#/VB .NET code, loading DOCX files, and accessing the object model is quick and straightforward.

You can add, remove, and modify objects (and their properties like formatting), save a file to a DOCX file, or export it to PDF. You can do all this in any .NET Standard 2.0 targeted application.

In this demo, we'll cover:

  • How to use the C# .NET API to load the Word document (.docx) file in DsWord
  • Use the PdfOutputSettings method of the C# .NET API to convert the Word document (.docx) to PDF

NOTE: Please be sure to review and/or create the sample code from the previous article: install DsWord in a .NET Core console application and create a Word document in code. Important dependencies and references will be required from this project to compile and run the steps below successfully.

Step 1: Install DsWord in .NET Core Console Application

Learn how to use DsWord.

After completing this example application, you should have a Word document that looks like the below:

convert

Step 2: Build On Existing Example - Add NuGet Package

To export a Word document to PDF, you need to download and install GrapeCity.Documents.Word.Layout NuGet Package.

Follow these steps:

Visual Studio for Windows

  1. Right-click the project in Solution Explorer and choose Manage NuGet Packages
  2. In Package source in the top right, select nuget.org
  3. Click the Browse tab in the top left and enter "GrapeCity.Documents" as the search string. You should see several GrapeCity.Documents packages listed
  4. Select GrapeCity.Documents.Word.Layout, and click Install. Accept the license agreement

Visual Studio for MAC

  1. In the Package source on the top left, choose nuget.org
  2. Click the Browse tab on the top right and search for "Grapecity.Documents"
  3. On the left panel, choose GrapeCity.Documents.Word.Layout
  4. On the right panel, click Install
  5. Choose 'I Accept' in the next screen

Visual Studio Code for Linux

  1. From Extensions, install the NuGet Package Manager and activate it
  2. In Visual Studio Code, press Ctrl+P to open the file command box, type > in it, find "NuGet Package Manager: Add Package" in the list that opens, and click it
  3. In the search box that opens, type "GrapeCity" and press Enter. This should bring up the list of available GrapeCity packages, GrapeCity.Documents.Word.Layout among them
  4. Select it and then the build number. The package will be added to the application

Step 3: Add Namespace

Make sure you have the following namespaces included in Program.cs file (if you are building on the previous application, some of these should already be in your code):

using GrapeCity.Documents.Word; using System.Drawing; using GrapeCity.Documents.Word.Layout; using System.IO.Compression;

Step 4: Load a Word Document (.docx) File in DsWord

Add the following lines of code in the main function of Program.cs. This code will create a DsWord document object. Then load the Word document into the object.

var wordDoc = new GcWordDocument();

var path = Path.Combine(@"ImportanceOfWetlands.docx");

wordDoc.Load(path);

Step 5: Export the Word Document (.docx) to PDF with Various PdfOutputSettings

Instead of saving to Word, you can directly save the Word document to PDF with the following PdfOutputSettings:

  • CompressionLevel: This specifies values that indicate whether a compression operation emphasizes speed or compression size. It takes the CompressionLevel Enum to set different values
  • PdfAConformanceLevel: It gets or sets the PDF/A conformance levels with the help of PdfAConformanceLevel Enum
  • DocumentInfo: The DocumentInfo object contains various information about the document like author, title, creation date, etc.
  • ImageOptions: It gets and sets the ImageOptions object that contains options controlling 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 gets or sets the background color. The default is white

Use the C# .NET and GcWordLayout class code snippet below to save the document to PDF:

using (var layout = new GcWordLayout(wordDoc))
   {

    PdfOutputSettings pdfOutputSettings = new PdfOutputSettings();

     pdfOutputSettings.CompressionLevel = CompressionLevel.Fastest;

     pdfOutputSettings.ConformanceLevel = GrapeCity.Documents.Pdf.PdfAConformanceLevel.PdfA1a;

     pdfOutputSettings.BackColor = Color.LightGray;

     pdfOutputSettings.DocumentInfo = new GrapeCity.Documents.Pdf.DocumentInfo()

     {   Author = "John",

         Title = "Deforestation",

        CreationDate = new GrapeCity.Documents.Pdf.PdfDateTime(DateTime.Now)

     };

    layout.SaveAsPdf("SimpleText.pdf", null, pdfOutputSettings);

   }

Run the application. The new PDF should look like this:

convert

Thanks for following along! If you have any questions about the new features or how to use any Document Solutions APIs, please contact us directly or leave comments on this blog.

Watch the video tutorial

comments powered by Disqus