|
Word OLE CSharp |
Top Previous Next |
|
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.
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 novapiLib;
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 = "<%SDK_SAMPLE_PRINTER%>"; PUBLIC STATIC STRING NOVAPDF_INFO_SUBJECT = "Document Subject"; PUBLIC STATIC STRING PROFILE_NAME = "Test C# OLE"; PUBLIC STATIC INT PROFILE_IS_PUBLIC = 0; PUBLIC STATIC uint NV_PROFILE_EXISTS = 0xD5DA0006;
[STAThread] STATIC VOID Main(STRING[] args) { try { // create the NovaPdfOptions object NovaPdfOptions pNova = new NovaPdfOptions(); // initialize the NovaPdfOptions object // if you have an application license for novaPDF SDK, // pass both the registration name and the license key to the Initialize() function // pNova.Initialize(PRINTER_NAME, "<registration name>", "<license key>", "<application name>"); pNova.Initialize(PRINTER_NAME, "", "", ""); // get the active profile ... STRING activeProfile; INT nActivePublic; pNova.GetActiveProfile(out activeProfile, out nActivePublic); try { // and make a copy of it pNova.CopyProfile(activeProfile, PROFILE_NAME, PROFILE_IS_PUBLIC); } catch (System.Runtime.InteropServices.COMException e) { // ignore profile exists error IF (NV_PROFILE_EXISTS == e.ErrorCode) { System.Console.WriteLine("Profile Test C# OLE already exists"); } ELSE { // more serious error, propagate it throw e; } } // set the copy profile as active profile ... pNova.SetActiveProfile(PROFILE_NAME, PROFILE_IS_PUBLIC); // and set some options pNova.SetOptionString(NOVAPDF_INFO_SUBJECT, "C# Hello document", PROFILE_NAME, PROFILE_IS_PUBLIC); // set nova default printer pNova.SetDefaultPrinter(); // initialize OLE usage in novaPDF pNova.InitializeOLEUsage("Word.Application"); // create Word application object WORD._Application WordApp = new WORD.Application(); WordApp.DisplayAlerts = 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:\test.doc"; // create Word document object 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 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; // restore active profile pNova.SetActiveProfile(activeProfile, nActivePublic); pNova.DeleteProfile(PROFILE_NAME, PROFILE_IS_PUBLIC); // restore default printer pNova.RestoreDefaultPrinter(); } catch (System.Runtime.InteropServices.COMException e) { MessageBox.Show(e.Message); } catch (Exception e) { MessageBox.Show(e.Message); } } } } |