Convert Word document to PDF in C#
How To C Sharp

Convert Word document to PDF in C#

Editorial Team
Editorial Team

Follow us on Discord or Telegram to get access to free tutorials and updates.

If you have used newer versions of MS Office, you might have noticed that we can save a Word document or Excel sheet as PDF. This is a useful feature. But how can we do this programmatically?

This post shows how to convert a Word document to PDF grammatically using C#.

To work with Word files, we need to add reference to Microsoft.Office.Interop.Word.dll first. Install it with the following command or you can also get it from NuGet.

Install-Package Microsoft.Office.Interop.Word -Version 15.0.4797.1003

Next, add required namespaces:

using Microsoft.Office.Interop.Word;

Convert Word to PDF

Here's a small program that converts a Word file to PDF.

using Microsoft.Office.Interop.Word;
using System;

namespace ConsoleApp
    class Program
        static void Main(string[] args)
            Application app = new Application();
            Document doc = app.Documents.Open(@"D:/test.docx");
            doc.SaveAs2(@"D:/test.pdf", WdSaveFormat.wdFormatPDF);

Please note that a new WINWORD.exe process is started each time when you run the code. This will eat up all the memory if the Close() method is not called.

This package offers several other features also. For example, we can count the number of characters or grammatical errors in a word file.

Count the number of characters:

var c = doc.Characters;

Count grammatical errors:

var e = doc.GrammaticalErrors;

Note: Like Word files, we can also convert Excel sheets and PPT files to PDF by adding appropriate packages.