0

I know my question is not focused on a precise problem but Google ran out of results. I'm trying to make a little app in Visual Basic .Net and have a HTML string which needs to be printed to a specific printer, and the problem is that i've tried to:

  • write out to a HTML file then print it with a WebBrowser: the problem is that i can't print to a specific printer, only to the default one.
  • convert it to a PDF with htmlToPdf package but: (1) it needs Acrobat Reader AND file association in Windows, (2) it opens the Acrobat Reader which is not quite professional.

EDIT

Thanks to a solution provided by the first commenter, i've made it partially. The problem is that the first document is printed perfectly, but the next ones are printed to the first specified printer (in Devices and Printers the default printer changes but the target printer remains the first one) Here is the code:

Public Sub PrintHTML(ByVal text As String, ByVal printer As String)
    Try
        LogData("Changing default printer to " & printer)
        Dim wtype = Type.GetTypeFromProgID("WScript.Network")
        Dim instance = Activator.CreateInstance(wtype)
        wtype.InvokeMember("SetDefaultPrinter", System.Reflection.BindingFlags.InvokeMethod, Nothing, instance, New Object() {printer})
    Catch ex As Exception
        LogData("Changing failed...")
        LogData(ex.ToString)
    Finally
        LogData("PrintHTML Init with " & printer)
        Me.wbForPrint.Navigate("about:blank")
        If Not WebBrowserReadyState.Interactive = WebBrowserReadyState.Complete Then
            Me.wbForPrint.Stop()
        End If
        Me.wbForPrint.DocumentText = text
        AddHandler (Me.wbForPrint.DocumentCompleted), AddressOf HTMLDocumentCompleted
    End Try

End Sub
K Attila
  • 33
  • 6

1 Answers1

0

If the printing needs to be automatic with any user input then you could use this code to change the default printer, and then restore the default printer back to what it was once you have done the printing (source: http://codesnippets.fesslersoft.de/how-to-set-the-default-printer-in-c-and-vb-net/)

Public Shared Sub SetDefaultPrinter(ByVal printername As String)
        Dim type As var = Type.GetTypeFromProgID("WScript.Network")
        Dim instance As var = Activator.CreateInstance(type)
        type.InvokeMember("SetDefaultPrinter", System.Reflection.BindingFlags.InvokeMethod, Nothing, instance, New Object() {printername})
    End Sub

Or if you want the user to choose which printer to send to, you could try:

 WebBrowser1.ShowPrintPreviewDialog()

or

 WebBrowser1.ShowPrintDialog()
user4574834
  • 114
  • 5
  • Thank you for the solution. I've encountered another problem: the first document is printer perfectly, but the next ones are printed to the first specified printer (in Devices and Printers the default changes but the target printer remains the first one). I've updated the code. Thank you! – K Attila Nov 23 '17 at 19:47
  • Maybe this will help you https://stackoverflow.com/questions/2561078/printing-from-webbrowser-control-prints-to-wrong-printer-after-setting-default – user4574834 Nov 24 '17 at 17:21