Felipe Gentil

Convert a MS Word file to PDF using C#

20 Mar 2012
c#

A couple of weeks has passed since my last post, but here I am again to give you a very easy and nice way to convert a Word file (by Word, read .docx, .doc, .rtf, etc) to a PDF. I have searched a lot on the internet about conversion to PDF and it’s not something easy, although it is simple to understand this code I am going to give…

First of all, to get this tutorial working correctly, you MUST have Office installed in your computer. Otherwise it won’t work. You have plenty of other tutorials even using iTextSharp to create your PDF or Java using JodConverter and OpenOffice like this.

So let’s get it started.

We create a new project and add Microsoft.Office.Interop.Word as refference like showing in the pictures bellow.

zsh

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

I created a form just to make things easier, but what really matters in this tutorial is the method called ConvertWord2PDF.

zsh

Okay, form already setup. We should go to our main method. Basically, to convert a Word file to PDF we must:

  • Open
  • Export
  • Close

So, that’s what we have by following this order.

private void ConvertWord2PDF(string inputFile, string outputPath)
{
    try
    {
        if (outputPath.Equals("") || !File.Exists(inputFile))
        {
            throw new Exception("Either file does not exist or invalid output path");
        }

        // app to open the document belower
        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        Document wordDocument = new Document();

        // input variables
        object objInputFile = inputFile;
        object missParam = Type.Missing;                

        wordDocument = wordApp.Documents.Open(ref objInputFile, ref missParam, ref missParam, ref missParam,
            ref missParam, ref missParam, ref missParam, ref missParam, ref missParam, ref missParam,
            ref missParam, ref missParam, ref missParam, ref missParam, ref missParam, ref missParam);

        if (wordDocument != null)
        {
            // make the convertion
            wordDocument.ExportAsFixedFormat(outputPath, WdExportFormat.wdExportFormatPDF, false,
                WdExportOptimizeFor.wdExportOptimizeForOnScreen, WdExportRange.wdExportAllDocument,
                0, 0, WdExportItem.wdExportDocumentContent, true, true,
                WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missParam);                                                    
        }

        // close document and quit application
        wordDocument.Close();
        wordApp.Quit();

        MessageBox.Show("File successfully converted");
        ClearTextBoxes();
    }
    catch (Exception e)
    {                
        throw e;
    }
}

Well, that’s pretty much what you need. I just want to focus on both methods, Open and ExportAsFixedFormat. There’s a bunch of parameters we are not using, if you want to understand one by one, all you need is to read the documentation.

Here we have our Open method.

zsh

And here the ExportAsFixedFormat method.

zsh

Finally, testing our application and…

zsh

Easy, huh? I know its restriction about the need of Microsoft Office installed but it’s the simplest way I have ever seen.Hope you guys get use of this tutorial, you can download the whole application here.

Thanks for all the attention.