Word OLE CSharp - SDK sample

Download Sample
Download size: 45.6KB
[Format: ZIP]
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.

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 = "novaPDF Pro v5";
   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);
     }
   }
}
}

© 2002-2008 Softland. All Rights Reserved.
PDF Creator novaPDF
Create PDF for your paperless office
home Home   contact usContact us   partnershipsPartnerships   bookmarkBookmark   Site updated: 8-October-2008
Downloads Download Now

Download, install and use novaPDF. Latest version: 5.5 build 276

Download PDF creator
promotionsSpecial Offers

Current discounts:
- up to 25% for Desktop Editions
- up to 25% for Server Editions
- 20% Academic Discount
- up to 16% for Developer Tools

buyBuy Now
Professional edition $39.95
(Save $10)
Standard edition $29.95
(Save $10)
Lite edition $19.95
(Save $5)
Buy Now
newsLatest News

novaPDF 5.5 released
August 28, 2008

What’s new (read more):
 » Added Import/Export profiles option
 » Added external function exit code to cancel print job in novaPDF OEM
 » Added "/RegisterWin32COM" install parameter for silent installers
 » Added "/DoNotAskReason" command line parameter for silent uninstalling
 » Added new large printing forms (A0)
 » Added Norwegian and Turkish interface languages

sdkDeveloper Tools

novaPDF SDK is a software development toolkit that can be used by software developers and programmers to add the ability to create PDF files in their applications.
novaPDF OEM is a PDF solution that programmers can use to generate PDF files that will be post-processed by their applications. […]

Reviews & User QuotesPDF Creator Reviews
"novaPDF is a really neat tool. Basically, it’s a PDF printer driver that prints to a PDF file instead of paper.
See more reviews about nova PDF
[…] novaPDF is a great product that works without forcing the user to configure anything. The convenience alone is worth the money." […]
Jason Pullara, Editor
Currently supported languagesLanguages

News:
- Norwegian language added.

Help us translate novaPDF and we will give you licenses for novaPDF Professional and mention you on the Languages page. […]

awardsAwards

novaPDF received the Editor’s Pick from Soft32, a major download site:

PDF creator reviews

See the Awards page for more ratings.

NewsletterKnowledgebase articles

Latest articles on how to:
 » Create PDF files
 » Convert JPG to PDF
 » Convert Powerpoint to PDF
 » Convert Publisher to PDF
 » Convert web pages to PDF
Full article list in the knowledgebase.