Limited time promo

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

Download now Buy licenses
Adrian (Softland)
Mar 28, 2022
novaPDF 11.x
We strive to keep our help 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 SDK.
It demonstrates the basic use of the INovaPDFOptions interface. The printing job is made using the package "System.Drawing.Printing"
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<%VERSION_MAJOR%>;
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 <%VERSION_MAJOR%>";
        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";

        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                // create the NovaPdfOptions object
                NovaPdfOptions<%VERSION_MAJOR%> pNova = new NovaPdfOptions<%VERSION_MAJOR%>();
                // initialize the NovaPdfOptions object
                pNova.Initialize(PRINTER_NAME, "");

                // get the active profile ...
                string activeProfile = null;
                bool isActiveProfile = true;
                //pNova.GetActiveProfile(out activeProfile, out nActivePublic);
                try
                {
                    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");
                    }
                    else
                    {
                        // 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
                pNova.LoadProfile(_newProfileID);
                //nova.NovaTools.AddProfileOptions(pNova);

                // and set some	options
                // uncomment the function calls for the options you wish to set
                // change the options in the nova.cs unit
                //nova.NovaTools.AddProfileOptions(pNova);
                //nova.NovaTools.AddDocumentInformation(pNova);
                //nova.NovaTools.AddViewerOptions(pNova);
                //nova.NovaTools.AddLinksOptions(pNova);
                //nova.NovaTools.AddAdvancedOptions(pNova);
                //nova.NovaTools.AddGraphicsOptions(pNova);
                //nova.NovaTools.AddSecurityOptions(pNova);
                //nova.NovaTools.AddSaveOptions(pNova);
                //nova.NovaTools.AddAfterSaveActions(pNova);
                //nova.NovaTools.AddEmailOptions(pNova);
                //nova.NovaTools.AddWatermarkImage(pNova);
                //nova.NovaTools.AddWatermarkText(pNova);
                //nova.NovaTools.AddPageContentOptions(pNova);
                //nova.NovaTools.AddOverlayOptions(pNova);
                //nova.NovaTools.AddSignatureOptions(pNova);
                //nova.NovaTools.AddEmbedFontsOptions(pNova);
                //nova.NovaTools.AddBookmarksDefinitions(pNova);

                //save the new added profile
                pNova.SaveProfile();
                //set the new profile as the active profile
                pNova.SetActiveProfile(_newProfileID);

                // 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);
                    pd.Print();
                }

                if (isActiveProfile)
                {
                    pNova.SetActiveProfile(activeProfile);
                }
                pNova.DeleteProfile(_newProfileID);
            }
            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;
        }
    }
}