Sample how to print a *"Hello World"* pdf file from VB.NET using novaPDF Printer.
Hello World VBNet sample is a simple Windows console application that prints one page with the *"novaPDF says Hello World from VB.Net"* text to the novaPDF Printer. It demonstrates the basic use of the
INovaPDFOptions
interface with this type of code.NoteTo be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: nova PDF SDK.
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 VBNet"*, 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. NoticeBecause of the specific exception based error handling in .NET, all calls to methods in theINovaPDFOptions
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 callpNova.AddProfile("test")
will throw anSystem.Runtime.InteropServices.COMException
with theErrorCode
property set toNV_PROFILE_EXISTS
(0xD5DA0006).
Source Code
Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms
' the novapiLib package must be added as a COM refference
Imports novapiLib80
Module Module1
' <summary>
' The main entry point for the application.
' </summary>
Const PRINTER_NAME As String = "novaPDF SDK 8"
Const PROFILE_NAME As String = "Test VBNet"
Const PROFILE_IS_PUBLIC As Integer = 0
Const NOVAPDF_INFO_SUBJECT As String = "Document Subject"
Const NV_PROFILE_EXISTS As Long = -707133434
Sub Main()
Try
' create the NovaPdfOptions object
Dim pNova As NovaPdfOptions80
pNova = New NovaPdfOptions80
' initialize the NovaPdfOptions object
' if you have an application license for novaPDF SDK,
pNova.Initialize(PRINTER_NAME)
' mark start changing options
' get the active profile ...
Dim activeProfile As String = ""
Dim nActivePublic As Integer = 0
Try
pNova.GetActiveProfile(activeProfile)
Catch ex As System.Runtime.InteropServices.COMException
' ignore profile exists error
If (NovaErrors.NV_NO_ACTIVE_PROFILE = ex.ErrorCode) Then
System.Console.WriteLine("The printer does not have an active profile")
Else
'more serious error, propagate it
Throw ex
End If
End Try
Dim newProfileID As String = ""
Try
' and make a copy of it
pNova.AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC, newProfileID)
Catch e As System.Runtime.InteropServices.COMException
' ignore profile exists error
If (NV_PROFILE_EXISTS = e.ErrorCode) Then
System.Console.WriteLine("Profile already exists")
Else
' more serious error, propagate it
Throw e
End If
End Try
'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 copy profile as active profile ...
pNova.SetActiveProfile(newProfileID)
' print a test page, using the previously set active profile settings
Dim pd As PrintDocument = New PrintDocument
pd.PrinterSettings.PrinterName = PRINTER_NAME
AddHandler pd.PrintPage, AddressOf PrintPageFunction
pd.Print()
If ((activeProfile.Length > 0) And (activeProfile.CompareTo(PROFILE_NAME) <> 0)) Then
pNova.SetActiveProfile(activeProfile)
pNova.DeleteProfile(newProfileID)
End If
Catch e As System.Runtime.InteropServices.COMException
MessageBox.Show(e.Message)
Catch e As Exception
MessageBox.Show(e.Message)
End Try
End Sub
' and finally the function that actually prints the page
Private Sub PrintPageFunction(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim str As String = "novaPDF says Hello World from VB.Net"
Dim font As Font = New Font("Arial", 16)
Dim brush As Brush = New SolidBrush(Color.Black)
ev.Graphics.DrawString(str, font, brush, 20.0!, 20.0!)
ev.HasMorePages = False
End Sub
End Module