none
Printing Documents (doc, xls, pdf, jpeg, etc) to a specific printer RRS feed

  • Question

  • Can anyone help with this printing issue.  I want to be able to print various kinds of documents to a specific printer.  I have looked at the PrintDocument class and the Process class.  My closest working attempt has been with the Process Class.

    Here is my code using a Process Class:

    Dim Proc As New Process
    
    Dim PrintTo As String = "\\dev003\NetPrintQueue2Fax"
    
    Dim outfile As String = Directory & "\" & FileName
    
    Dim psi As New ProcessStartInfo("AcroRd32.exe", "/t" + outfile + " " + PrintTo + " ")
    
    Try
    
    psi.CreateNoWindow = True
    
    psi.UseShellExecute = False
    
    psi.Verb = "print"
    
    psi.FileName = "C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"
    
    psi.WorkingDirectory = "C:\Program Files\Adobe\Reader 9.0\Reader\"
    
    psi.RedirectStandardOutput = True
    
    psi.WindowStyle = ProcessWindowStyle.Normal
    
    Proc.StartInfo = psi
    
    Proc.Start()
    
    Catch ex As System.Exception
    
    Dim ErrorRpt As New ErrorReporting.ErrorReporting
    
    ErrorRpt.Log_Errors(ex)
    
    MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Raised")
    
    End Try
    
    

    My ulimate goal is to be able to fax these documents through a Xerox copier that has printing and faxing capabilities.

    Thank You


    Michael J Hill
    Tuesday, August 17, 2010 8:13 PM

Answers

  • Thank you Mike for your friendly help and good suggestion.

     

    Hi Michael,


    Welcome to MSDN forums!

     

    Mike pointed you to the right direction.

    The following is the detailed code sample: How to print an external document using Process.Start method in VB.NET.

     

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim psi As New ProcessStartInfo

            psi.UseShellExecute = True

            psi.Verb = "print"

            psi.WindowStyle = ProcessWindowStyle.Hidden

    psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()' Here specify printer name

            psi.FileName = "C:\MyFile.doc" ' Here specify a document to be printed

            Process.Start(psi)

        End Sub

    End Class

     

    Best regards,

    Guang-Ming 

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Wednesday, August 18, 2010 6:23 AM
    Moderator

All replies

  • Your code did not appear to use a filename which you wanted printed.
     
    This is the simplest way to print a generic file to the default printer:
     
            Dim Proc As New Process
            Proc.StartInfo.FileName = "Somefile.pdf"
            Proc.StartInfo.Verb = "Print"
            Proc.Start()
     
    I believe you would need to set the default printer to the one you wish before this code.  I don't think it can be set for the process object.

    --
    Mike
    Wednesday, August 18, 2010 2:51 AM
  • Thank you Mike for your friendly help and good suggestion.

     

    Hi Michael,


    Welcome to MSDN forums!

     

    Mike pointed you to the right direction.

    The following is the detailed code sample: How to print an external document using Process.Start method in VB.NET.

     

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim psi As New ProcessStartInfo

            psi.UseShellExecute = True

            psi.Verb = "print"

            psi.WindowStyle = ProcessWindowStyle.Hidden

    psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()' Here specify printer name

            psi.FileName = "C:\MyFile.doc" ' Here specify a document to be printed

            Process.Start(psi)

        End Sub

    End Class

     

    Best regards,

    Guang-Ming 

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Wednesday, August 18, 2010 6:23 AM
    Moderator
  • Hi,

    I used your code to print a Word document, but when I execute the code it ignores the PrinterSettings and prints out the file to my standard printer.

    The only difference is that I use a PrinterSettings object instead of a PrintDialog object.

    Here is my code:

    Dim settings As New PrinterSettings
    Dim psi As New ProcessStartInfo
                            
    settings.PrinterName = "\\SERVER\Canon C2220 A4"
    
    psi.UseShellExecute = True
    psi.Verb = "print"
    psi.WindowStyle = ProcessWindowStyle.Hidden
    psi.Arguments = settings.PrinterName.ToString()
    psi.FileName = pad
    
    Process.Start(psi)
    Do I need the printDialog object?

    Friday, February 20, 2015 12:59 PM
  • Hi,

    I used your code to print a Word document, but when I execute the code it ignores the PrinterSettings and prints out the file to my standard printer.

    The only difference is that I use a PrinterSettings object instead of a PrintDialog object.

    Here is my code:

    Dim settings As New PrinterSettings
    Dim psi As New ProcessStartInfo
                            
    settings.PrinterName = "\\SERVER\Canon C2220 A4"
    
    psi.UseShellExecute = True
    psi.Verb = "print"
    psi.WindowStyle = ProcessWindowStyle.Hidden
    psi.Arguments = settings.PrinterName.ToString()
    psi.FileName = pad
    
    Process.Start(psi)
    Do I need the printDialog object?

    You should just start a new question instead of redoing an old one?

    Here is another complete example that prints a file to the xps  printer. You would change the name.

    Edit: also remove the printtofile etc.

    'converts text file to xps file format
    Imports System.IO
    Imports System.Drawing.Printing
    
    Public Class PrintXPSfile
        Private theText As String
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Try
                'read the file
                Using sr As New StreamReader("c:\textfiles\durango.txt")
                    theText = sr.ReadToEnd()
                End Using
            Catch ex As Exception
                MsgBox("Problem reading file" & vbLf & ex.Message)
            End Try
    
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Try
                Using pd As New System.Drawing.Printing.PrintDocument
                    With pd
                        AddHandler .PrintPage, AddressOf Me.PrintPageHandler
                        .DefaultPageSettings.Landscape = True
                        .DefaultPageSettings.PaperSize = New PaperSize("Legal", 850, 1400)
    
    
                        'print as xps file to disc
                        .PrinterSettings.PrinterName = "Microsoft XPS Document Writer"
                        .DefaultPageSettings.PrinterSettings.PrintToFile = True
                        .DefaultPageSettings.PrinterSettings.PrintFileName = "C:\textfiles\durango.xps"
    
                        .Print()
    
                        RemoveHandler .PrintPage, AddressOf Me.PrintPageHandler
                    End With
                End Using
    
            Catch ex As Exception
                MsgBox("Problem writing file" & vbLf & ex.Message)
            End Try
    
        End Sub
        Private Sub PrintPageHandler(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    
            Using f As Font = New Font(New FontFamily("Arial"), 12, FontStyle.Bold, Drawing.GraphicsUnit.Pixel)
                e.Graphics.DrawString(theText, f, Brushes.Black, e.MarginBounds)
            End Using
    
            'e.HasMorePages = True ELSE e.HasMorePages = False
    
        End Sub
    
    End Class


    Friday, February 20, 2015 1:46 PM
  • This answer is not helpful. The document I want to print is a very complex document. I can't just read it into a string to write it on a PrintDocument object. That's why I used the code that I found on this page.

    I replied here so my code can be compared to the answer that was given earlier. It's just that with my code the document is printed by my standard printer instead of the printer I choose. I just want to know how I can fix my code to print the document with that specific printer.

    Friday, February 20, 2015 2:46 PM
  • I have EXACTLY the same issue. Document is printed to my default printer and the chosen printer name is ignored. Please let me know if you found the answer! Thank you!
    Tuesday, February 24, 2015 7:26 PM
  • Thank,

    I use this code with other application and it is working fine.


    Wednesday, September 21, 2016 11:12 AM
  • Hi , you could use Spire.Office component to print Word, Excel PDF, PowerPoint file to a specific printer as well as set the print options.

    http://officeapi.codeplex.com/

    Friday, September 23, 2016 2:35 AM