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.

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.

It demonstrates the basic use of the INovaPDFOptions interface. The printing job is made using the package "System.Drawing.Printing"

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.

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 novapiLib80;
using Globals;

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 string PROFILE_NAME = "HelloWorld";
        public static int PROFILE_IS_PUBLIC = 0;

        public static string PDF_TEXT = "Hello World";
        public static string PDF_FILE_NAME = "HelloWorld.pdf";

        static void Main(string[] args)
                // create the NovaPdfOptions object
                NovaPdfOptions80 pNova = new NovaPdfOptions80();
                // initialize the NovaPdfOptions object
                pNova.Initialize(PRINTER_NAME, "");

                // get the active profile ...
                string activeProfile = null;
                bool isActiveProfile = true;
                //pNova.GetActiveProfile(out activeProfile, out nActivePublic);
                    pNova.GetActiveProfile(out activeProfile);
                catch (System.Runtime.InteropServices.COMException e)
                    isActiveProfile = false;
                    // ignore profile exists error
                    if (NovaErrors.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;

                string _newProfileID = String.Empty;
                //add a new profile
                pNova.AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC, out _newProfileID);

                //load the new profile

                // and set some    options
                // uncomment the function calls for the options you wish to set
                // change the options in the nova.cs unit

                //save the new added profile
                //set the new profile as the active profile

                // 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.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 826, 1169);

                if (isActiveProfile)
            catch (System.Runtime.InteropServices.COMException e)
            catch (Exception e)
        // 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;