Hello World (C#) - SDK sample


Related Articles Add comment Email article Print article

Hello World CSharp sample is a simple Windows console application that prints one page with the "novaPDF says Hello World from C#" text to the novaPDF Printer. It demonstrates the basic use of the INovaPDFOptions interface. The printing job is made using the package "System.Drawing.Printing"

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.

Basically the sample determines the active profile, makes a copy of it into a profile called "Test C#", sets the new profile as active, sets the subject of the generated PDF document, prints a page, and restores original printer settings. The location of the generated document depends on whatever the settings are for the current active profile.

Notice
Because of the specific exception based error handling in .NET, all calls to methods in the INovaPDFOptions interface must be nested within a try-catch block. Consider for example that we want to add a profile called "test", but the profile "test" already exists. Then the call pNova.AddProfile("test") will throw an "System.Runtime.InteropServices.COMException". with the ErrorCode property set to NV_PROFILE_EXISTS (0xD5DA0006).

Source code

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
// the novapiLib package must be added as a        COM        refference
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 v6";
   public static string NOVAPDF_INFO_SUBJECT =        "Document Subject";
   public static uint         NV_PROFILE_EXISTS = 0xD5DA0006;
   public static string PROFILE_NAME =        "Test C#";
   public static int         PROFILE_IS_PUBLIC = 0; 

   [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# 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); 

       // print a test        page, using        the        previously set active profile settings
       using (PrintDocument pd = new PrintDocument())
       {
         pd.PrinterSettings.PrinterName = PRINTER_NAME;
         pd.PrintPage += new PrintPageEventHandler(PrintPageFunction);
         pd.Print();
       } 

       pNova.SetActiveProfile(activeProfile, nActivePublic);
       pNova.DeleteProfile(PROFILE_NAME, PROFILE_IS_PUBLIC);
     }
     catch (System.Runtime.InteropServices.COMException e)
     {
       MessageBox.Show(e.Message);
   }
     catch (Exception e)
     {
       MessageBox.Show(e.Message);
     }
   }
   // and finally the function that actually prints the page
   private static void        PrintPageFunction(object sender, PrintPageEventArgs        ev)
   {
     string str = "novaPDF says Hello World from C#";
     Font font =        new        Font("Arial", 16);
     Brush brush        = new SolidBrush(Color.Black);
     ev.Graphics.DrawString(str, font, brush, 20.0f, 20.0f);
     ev.HasMorePages = false;
   }
}
}


Related articles

Comments