Limited time promo

Word OLE CSharp - SDK sample

English en

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

Download now Buy licenses
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;

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

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

					try
					{
					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");
					}
					else
					{
						// more    serious error, propagate it
						throw e;
					}
				}

				//add a new profile
				pNova.AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC, out _newProfileID);
				//load the new profile
				pNova.LoadProfile(_newProfileID);
				// and set some    options
				pNova.SetOptionString(NOVAPDF_INFO_SUBJECT, "C# Hello document");
				// save profile
				pNova.SaveProfile();
				// set the copy    profile as active profile ...
				pNova.SetActiveProfile(_newProfileID);
				// set nova default printer
				pNova.SetDefaultPrinter();
				// initialize OLE usage in novaPDF
				pNova.InitializeOLEUsage("Word.Application");
				// 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
				pNova.LicenseOLEServer();
				// 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)
			{
				MessageBox.Show(e.Message);
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message);
			}
			// restore active profile
			if ((activeProfile.Length > 0) && (activeProfile.CompareTo(PROFILE_NAME) != 0))
			{
				pNova.SetActiveProfile(activeProfile);
				pNova.DeleteProfile(_newProfileID);
			}
			// restore default printer
			pNova.RestoreDefaultPrinter();
		}
	}
}