Limited time promo

This article applies only to novaPDF. If you don't have it yet, you must download it first.

Download now Buy licenses
Lorant (Softland)
Feb 25, 2019

We strive to keep our articles as accurate as possible. If you notice any inconsistencies or outdated info please let us know.

Convert a MS Word document to pdf using Word OLE automation.

The Word OLE CSharp sample is a simple Windows console application that converts a MS Word document (C:\Test.doc) to PDF using Word OLE automation. This can be a part of a larger application that works seamlessly with the novaPDF SDK development.

Note: To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: nova PDF SDK.

Source code

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
// the novapiLib package must be added as a    COM reference
using novapiLib80;

namespace Hello_World_CSharp
    ///    <summary>
    ///    Summary description for Class1.
    ///    </summary>
    class Class1
        ///    <summary>
        ///    The main entry point for the application.
        ///    </summary>
        public static string PRINTER_NAME = "novaPDF SDK 8";
        public static int NOVAPDF_INFO_SUBJECT = 68;
        public static string PROFILE_NAME = "Test C# OLE";
        public static int PROFILE_IS_PUBLIC = 0;
        public static uint NV_PROFILE_EXISTS = 0xD5DA0006;
        public static uint NV_NO_ACTIVE_PROFILE = 0xD5DA0028;

        static void Main(string[] args)
                // create the NovaPdfOptions8080 object
              NovaPdfOptions80 pNova = new NovaPdfOptions80();
                // get the active profile ...
              string activeProfile = "";
                string _newProfileID = String.Empty;

                // initialize the NovaPdfOptions8080 object
                // if you have an application license for novaPDF SDK, 
                pNova.Initialize(PRINTER_NAME, "");

                    pNova.GetActiveProfile(out activeProfile);
                        catch (System.Runtime.InteropServices.COMException e)
                    // ignore profile exists error
                    if (NV_NO_ACTIVE_PROFILE == (uint)e.ErrorCode)
                        System.Console.WriteLine("The printer does not have an active profile");
                        // more    serious error, propagate it
                        throw e;

                //add a new profile
                pNova.AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC, out _newProfileID);
                //load the new profile
                // and set some    options
                pNova.SetOptionString(NOVAPDF_INFO_SUBJECT, "C# Hello document");
                // save profile
                // set the copy    profile as active profile ...
                // set nova default printer
                // initialize OLE usage in novaPDF
                // create Word application object
                Microsoft.Office.Interop.Word._Application WordApp = new Microsoft.Office.Interop.Word.Application();
                WordApp.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
                // license OLE server in novaPDF
                // initializations
                object objMissing = System.Reflection.Missing.Value;
                object objTrue = true; object objFalse = false;
                object strFile = @"C:\temp\test.docx";
                // create Word document object
                Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref strFile, ref objFalse, ref objTrue,
                        ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                        ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                        ref objMissing);
                // print document
                WordApp.ActivePrinter = PRINTER_NAME;
                WordDoc.PrintOutOld(ref objFalse, ref objFalse, ref /\*refRange\*/objMissing, ref objMissing,
                    ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                    ref objFalse, ref objMissing, ref objMissing, ref objMissing);
                // close Word objects
                WordDoc.Close(ref objFalse, ref objMissing, ref objFalse);
                WordApp.Quit(ref objFalse, ref objMissing, ref objFalse);
                WordApp = null;

            catch (System.Runtime.InteropServices.COMException e)
            catch (Exception e)
            // restore active profile
            if ((activeProfile.Length > 0) && (activeProfile.CompareTo(PROFILE_NAME) != 0))
            // restore default printer