The VBNet Converter sample demonstrates how to convert an existing file by printing it to novaPDF SDK using the ShellExecute function. It also demonstrates how to set different options and manage profiles.
The same approach should be taken 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 SDK 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 SDK before calling ShellExecute (using the SetDefaultPrinter method), wait the process to be started (using WaitForExit()), 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 SDK.
Source code snippets
- Declare INovaPdfOptions variable
Private mobjNovaOptios As NovaPdfOptions80Class
- Initialize INovaPdfOptions
mobjNovaOptios = New NovaPdfOptions80Class ' initialize the NovaPdfOptions object mobjNovaOptios.Initialize(PRINTER_NAME) AddSmallProfile() AddFullProfile()
- Set novaPDF SDK printer options
Try Dim newSmallSizeProfileId As String = "" ' add new profile mobjNovaOptios.AddProfile(SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC, newSmallSizeProfileId) 'load the new profile mobjNovaOptios.LoadProfile(newSmallSizeProfileId) ' Set some options to this profile ' disable the "Save PDF file as" prompt mobjNovaOptios.SetOptionLong(NovaOptions.NOVAPDF_SAVE_PROMPT_TYPE, 0) ' set generated Pdf files destination folder "c:\" mobjNovaOptios.SetOptionString(NovaOptions.NOVAPDF_SAVE_FOLDER, "c:\\") // ..... Catch ComException As System.Runtime.InteropServices.COMException MessageBox.Show("Error creating Small Size Profile:" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + ComException.Message) System.Diagnostics.Debug.WriteLine(ComException.Message) End Try
- Start a Print job
Private Sub btnStartPrinting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartPrinting.Click
mobjNovaOptios.SetActiveProfile(CType((cmbProfiles.SelectedItem), String))
mobjNovaOptios.SetDefaultPrinter()
mobjNovaOptios.LicenseShellExecuteFile(txtFileToConvert.Text)
Dim myProcess As Process = New Process
Try
myProcess.StartInfo.FileName = txtFileToConvert.Text
myProcess.StartInfo.Verb = "Print"
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
myProcess.Start()
Catch ex As Win32Exception
If ex.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
Console.WriteLine(ex.Message + ". Check the path and filename")
Else
' Note that if your word processor might generate exceptions
' such as this, which are handled first.
If ex.NativeErrorCode = ERROR_ACCESS_DENIED Then
Console.WriteLine(ex.Message + ". You do not have permission to print this file.")
End If
End If
End Try
myProcess.WaitForExit(10000)
myProcess.Close()
mobjNovaOptios.RestoreDefaultPrinter()
End Sub