VB Converter

Top  Previous  Next

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.

 

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 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 NovaPdfOptions

 

2. Register novaPDF 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)

    Form1.Show

End Sub

 

' Sub that will handle the windows messages

Public Function VB_WindowProc(ByVal hwnd As LongByVal wMsg As LongByVal wParam As LongByVal lParam As LongAs 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 strProfile As String

    Dim strActiveProfile As String

 

    ' initialize the NovaPdfOptions object

    ' if you have an application license for novaPDF SDK,

    ' pass both the registration name and the license key to the Initialize() function

    ' m_NovaOptions.Initialize(PRINTER_NAME, '<registration name>', '<license key>');

    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)

 

    cmbProfiles.Clear

 

....

    Exit Sub

ErrorHandler:

    Debug.Print err.Number & ":" & err.Description

End Sub

 

4. Set novaPDF Printer Options

Private Sub AddSmallSize()

    On Error GoTo ErrHandler

 

    m_NovaOptions.AddProfile2 SMALL_SIZE_PROFILE, PROFILE_IS_Public

 

    ' Set some options to this profile

 

    ' disable the 'Save PDF file as' prompt

    m_NovaOptions.SetOptionLong2 NOVAPDF_SAVE_PROMPT, False, SMALL_SIZE_PROFILE, PROFILE_IS_Public

    ' set generated Pdf files destination folder  'c:\'

    m_NovaOptions.SetOptionString2 NOVAPDF_SAVE_FOLDER, "c:\", SMALL_SIZE_PROFILE, PROFILE_IS_Public

    ' set output file name

    m_NovaOptions.SetOptionString2 NOVAPDF_SAVE_FILE, "PDF Converter small size.pdf", SMALL_SIZE_PROFILE, PROFILE_IS_Public

    ' if file exists in the destination folder, append a counter to the end of the file name

    m_NovaOptions.SetOptionLong2 NOVAPDF_SAVE_CONFLICT_STRATEGY, FILE_CONFLICT_STRATEGY_AUTONUMBER_New, SMALL_SIZE_PROFILE, PROFILE_IS_Public

    ' don't detect URLs

    m_NovaOptions.SetOptionLong2 NOVAPDF_URL_ANALIZE, False, SMALL_SIZE_PROFILE, PROFILE_IS_Public

 

    ' Set image compression method to JPEG and quality to 75, possible values are from 10 to 100

    m_NovaOptions.SetOptionLong2 NOVAPDF_USE_IMAGE_COMPRESSION, True, SMALL_SIZE_PROFILE, PROFILE_IS_Public

    m_NovaOptions.SetOptionLong2 NOVAPDF_IMAGE_COMPRESSION_METHOD, COMPRESS_METHOD_JPEG, SMALL_SIZE_PROFILE, PROFILE_IS_Public

    m_NovaOptions.SetOptionLong2 NOVAPDF_IMAGE_COMPRESSION_LEVEL, 75, SMALL_SIZE_PROFILE, PROFILE_IS_Public

 

    ' make sure text compression is enabled, and set compression level to 9  maximum   possible values are 1-9

     m_NovaOptions.SetOptionLong2 NOVAPDF_USE_Text_COMPRESSION, True, SMALL_SIZE_PROFILE, PROFILE_IS_Public

     m_NovaOptions.SetOptionLong2 NOVAPDF_Text_COMPRESSION_LEVEL, 9, SMALL_SIZE_PROFILE, PROFILE_IS_Public

 

    ' disable unused font embedding

     m_NovaOptions.SetOptionLong2 NOVAPDF_EMBED_ALL_FONTS, False, SMALL_SIZE_PROFILE, PROFILE_IS_Public

    Exit Sub

ErrHandler:

    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 cmbProfiles.Text, PROFILE_IS_Public

    m_NovaOptions.SetDefaultPrinter

    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

    Exit Sub

.....

End Sub

 

6. Restore default printer when printing finished

Private Sub OnNovaPDFFileSaved(wParam As Long, lParam As Long)

    m_NovaOptions.UnRegisterEventWindow

    m_NovaOptions.RestoreDefaultPrinter

End Sub

 

Private Sub OnNovaPDFPrintError(wParam As Long, lParam As Long)

    Select Case wParam

        Case Error_MSG_TEMP_FILE:

            MsgBox "Error saving temporary file on printer server", vbOKOnly, "novaPDF"

        Case Error_MSG_LIC_INFO:

            MsgBox "Error reading license information", vbOKOnly, "novaPDF"

        Case Error_MSG_SAVE_PDF:

            MsgBox "Error saving PDF file", vbOKOnly, "novaPDF"

        Case Error_MSG_JOB_CANCELED:

            MsgBox "Print job was canceled", vbOKOnly, "novaPDF"

    End Select

    m_NovaOptions.UnRegisterEventWindow

    m_NovaOptions.RestoreDefaultPrinter

End Sub