Limited time promo

VB Converter - SDK sample

English en

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

Download now Buy licenses
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.
The VB Converter sample demonstrates how to convert an existing file by printing it to novaPDF Printer using the ShellExecute function. It also demonstrates how to set different options and manage profiles in the novaPDF SDK application.
To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: novaPDF SDK.

The same approach should be used if you print using a Print() method from another object (like an internet browser or a report control). Just replace the ShellExecute call with the call of your Print method.
When the application starts, it creates a few profiles and makes different settings in the profiles. Then it shows a dialog from where the user can select the active profile and change its settings using the controls from the dialog. After that a document can be selected from the harddisk and printed to novaPDF Printer using the ShellExecute function call.
When using this technique to convert a file to PDF, you have to take care of the fact that ShellExecute prints to the default printer. This function returns immediately and does not wait until the print is finished (it may return before the printing is actually started).
Therefore you have to set the default printer to novaPDF Printer before calling ShellExecute (using the SetDefaultPrinter method), register FileSaved message (or any other novaPDF Printer message) to be sure that the print job was started.
In this message handler restore the default printer (with the RestoreDefaultPrinter method). This way you made sure that the default printer was restored and your document is printed to novaPDF Printer.
Source Code Snippets
1. Declare INovaPdfOptions variable
'create the NovaPdfOptions object
Public m_NovaOptions As New NovaPdfOptions80

2. Register <%SDK_SAMPLE_PRINTER%> messages
Public wm_Nova_FileSaved As Long
Public wm_Nova_PrintError As Long

Sub Main()
	' Registering the messages send by the print in order to listen for them
	wm_Nova_FileSaved = RegisterWindowMessage(MSG_NOVAPDF2_FILESAVED)
	wm_Nova_PrintError = RegisterWindowMessage(MSG_NOVAPDF2_PRINTERROR)
End Sub

' Sub that will handle the windows messages
Public Function VB_WindowProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
	' For the registered messages perform specific tasks
	If wMsg = wm_Nova_FileSaved Then
		OnNovaPDFFileSaved wParam, lParam
		Exit Function
	End If
	If wMsg = wm_Nova_PrintError Then
		OnNovaPDFPrintError wParam, lParam
		Exit Function
	End If

	' For other messages just send them via normal (old) handling process
	VB_WindowProc = CallWindowProc(oldHandler, hwnd, wMsg, wParam, lParam)
End Function

3. Initialize INovaPdfOptions
Private Sub Form_Load()

	On Error GoTo ErrorHandler:
	Dim strProfileID As String
	Dim strProfileName As String
	Dim strActiveProfileID As String
	Dim nPublicProfile As Long
	Dim nActivePublicProfile As Long

	' initialize the NovaPdfOptions object to use with a printer licensed for SDK
	m_NovaOptions.Initialize (PRINTER_NAME, "")

	' sets the value of the windows messages handler to VB_WindowProc and sets the old handler address of function in oldHandler
	oldHandler = SetWindowLongApi(Me.hwnd, GWL_WNDPROC, AddressOf VB_WindowProc)


	Call AddSmallSize
	Call AddFullOptions

	' get the active profile ...
	m_NovaOptions.GetActiveProfile2 strActiveProfileID

	m_NovaOptions.GetFirstProfile2 strProfileID
	m_NovaOptions.LoadProfile2 strProfileID
	m_NovaOptions.GetOptionString2 NOVAPDF_PROFILE_NAME, strProfileName
	cmbProfiles.AddItem (strProfileName)

	On Error Resume Next
		m_NovaOptions.GetNextProfile2 (strProfileID)
		If err.Number = NV_NO_MORE_PROFILES Then
			Exit Do
		End If
		m_NovaOptions.LoadProfile2 strProfileID
		m_NovaOptions.GetOptionString2 NOVAPDF_PROFILE_NAME, strProfileName
		cmbProfiles.AddItem (strProfileName)
	Loop While True

	On Error GoTo ErrorHandler:

	cmbProfiles.ListIndex = 0

	Exit Sub
	Debug.Print err.Number & ":" & err.Description
End Sub

4. Set <%SDK_SAMPLE_PRINTER%> Options
Private Sub AddSmallSize()
	Dim strProfileID As String
	On Error GoTo ErrHandler

	m_NovaOptions.AddProfile2 SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC, strProfileID
	m_NovaOptions.LoadProfile2 (strProfileID)

	' Set some options to this profile

	' disable the "Save PDF file as" prompt
	' set generated Pdf files destination folder  "c:\"
	m_NovaOptions.SetOptionString2 NOVAPDF_SAVE_FOLDER, "c:\"
	' set output file name
	m_NovaOptions.SetOptionString2 NOVAPDF_SAVE_FILE_NAME, "PDF Converter small size.pdf"
	' if file exists in the destination folder, append a counter to the end of the file name
	' don't detect URLs
	m_NovaOptions.SetOptionLong2 NOVAPDF_URL_ANALIZE, False

	' Set image compresion method to JPEG and quality to 75, possible values are from 10 to 100
	m_NovaOptions.SetOptionLong2 NOVAPDF_COMPRESS_ENABLE, True
	m_NovaOptions.SetOptionLong2 NOVAPDF_COMPRESS_HIGH_COLOR, True

	' make sure text compression is enabled, and set compression level to 9  maximum   posible values are 1-9
	m_NovaOptions.SetOptionLong2 NOVAPDF_COMPRESS_TEXT_GRAPHICS, True

	' disable unused font embeding
	m_NovaOptions.SetOptionLong2 NOVAPDF_FONTS_EMBED_ALL_USED_FONTS, False

	'save profile changes

	Exit Sub
	If err.Number <> NV_PROFILE_EXISTS Then Debug.Print err.Number & ":" & err.Description

End Sub

5. Start a Print job
Private Sub btnStartPrinting_Click()
	m_NovaOptions.SetActiveProfile2 (strProfileID)
	Dim strToExecute As String
	Dim r As Long
	m_NovaOptions.RegisterEventWindow Me.hwnd
	m_NovaOptions.LicenseShellExecuteFile txtFileToConvert
	r = ShellExecute(Me.hwnd, "print", txtFileToConvert, "", "", SW_HIDE)
	btnStartPrinting.Enabled = True
End Sub

6. Restore default printer when printing finished
Private Sub OnNovaPDFFileSaved(wParam As Long, lParam As Long)
End Sub

Private Sub OnNovaPDFPrintError(wParam As Long, lParam As Long)
	Select Case wParam
			MsgBox "Error saving temporary file on printer server", vbOKOnly, "novaPDF"
			MsgBox "Error reading license information", vbOKOnly, "novaPDF"
			MsgBox "Error saving PDF file", vbOKOnly, "novaPDF"
			MsgBox "Print job was canceled", vbOKOnly, "novaPDF"
	End Select
End Sub