Print PDF file in VB.Net by giving Printer Name

VB.NET Code:


Dim pathToExecutable As String = "AcroRd32.exe"

Dim sReport = "C:Test.PDF" 'Complete name/path of PDF file

Dim SPrinter = "HP Officejet 5600 seriese" 'Name Of printer

Dim starter As New ProcessStartInfo(pathToExecutable, "/t " + sReport + " " + sPrinter + "")

Dim Process As New Process()

Process.StartInfo = starter Process.Start()
'try and close the process with 20 seconds delay
System.Threading.Thread.Sleep(30000)

Process.CloseMainWindow()
Dim iLoop As Int16 = 0
'check the process has exited or not
If Process.HasExited = False Then

'if not then loop for 100 time to try and close the process'with 10 seconds delay
While Not Process.HasExited
System.Threading.Thread.Sleep(10000)
Process.CloseMainWindow()
iLoop = CShort(iLoop + 1)

If iLoop >= 100 Then

Exit While
End If
End While
End If

Process.Close()
Process.Dispose()
Process = Nothing
starter = Nothing

 

About these ads
Comments
58 Responses to “Print PDF file in VB.Net by giving Printer Name”
  1. Dan says:

    The closewindow seams to work for the first PDF to print, but it doesn’t close the window for the second PDF with AdobeReader 7.0. Any ideas?

    Thanks,
    Dan

  2. Ramesh says:

    Hi

    I was searching the web to print a pdf file by giving printer name. I came accross your article. I am trying to do this in Visual Basic 6.0. Do you have any idea how it can be done ?
    Thanks.
    Ramesh.

  3. John says:

    Hello Ripal Soni,

    First of all, http://www.ripalsoni.wordpress.com is an excellent site for .NET Interview Question Answers.

  4. Neha says:

    This is what i was looking for
    Print PDF file in VB.Net by giving Printer Name

    too good :)

  5. Dinesh says:

    Hi Ripal,
    I do have a Similar requirement but i have to print an rtf file out , so wil this lil beauty work for that too?

  6. Michael says:

    Thank Ripel, saved me a it of coding. Works great

  7. rooz says:

    Hey there,
    I need to print the first page of my pdf on tray1 (it’s a named tray) and the rest of my pdf-pages on the 2nd tray…. any ideas?

    cheers!

  8. Jeremy says:

    I tried this code but nothing happens when I run it. I don’t get any errors and nothing prints out.

    rooz, any success with the multiple tray printing? I too have a report that needs to print from 2 diferent trays.

  9. bumble bee says:

    hi,

    I managed to print multiple pdfs file. It works. The first pdf files printed perfectly. But second pdf, the content’s font size and images size reduced (become smaller). Following is my code. Please advice.

    Thanks.

    While intArrayCount > intCounter
    Dim command As Process = New Process
    command.StartInfo.Verb = “print”
    command.StartInfo.FileName = arrFileToPrint.Item(intCounter).ToString
    command.StartInfo.CreateNoWindow = True
    command.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    command.Start()
    command.StartInfo.po()

    command.CloseMainWindow()
    command.Close()
    command.Dispose()
    command = Nothing

    intCounter += 1
    End While

  10. Jeremy says:

    Great article. But mine is acting oddly…

    It works with all of my real printers, but fails with any virtual printers

    Example
    if I pass
    \\server3\HP Color LaserJet 2600n
    or
    \\server5\HP LaserJet 1200 Series PCL

    …it works perfectly

    but If I pass the virtual printers
    Microsoft Office Document Image Writer
    or
    SnagIt 8

    Adobe throws the error:
    “There was an error opening this document. This file can not be found”

    Any ideas? thanks!

  11. durgesh says:

    hi Ripal,

    your code works in opening the pdf file but the

    printing is not started at all though no error is

    reported ,

    please help me

    Thanks in advance .

    durgesh
    bhubaneswar

  12. jj says:

    How do you know the path to the locally installed AcroRd32.exe?

  13. Ripal Soni says:

    Hello JJ

    the in Pathtoexecutable you just need to give your exe name which you want to execute ,i think it will find path by itself ,the general place where this should be is c:programfiles or c:acrobat folder

  14. Krishan says:

    I want to print a word document on Adobe PDF printer and want to save output file automatically.What will be ths process for that?

    Thanks in advance

  15. Kennie says:

    Nice….
    is it possible to change the font size smaller before print

  16. Tapan says:

    Hello, the above code “Print the pdf files by selecting the name of the printer” works fine.
    But additionaly if i want to abort the printing process in between then how can i implement it.
    This is my requirement.

    Please mail me

  17. sudhakar says:

    Dear Ripalsoni,

    This is sudhakar, I have requirement like printing a pdf file in different trays of single printer.

    eg:-

    Suppose a pdf file has 100 pages. and a printer has 3 trays.

    our Service should able to print 1 to N in tray1,
    N+1 to M in tray2 and
    M+1 to 100 in tray3.

    Where N and M are whole numbers with in the range of 2 to 99.

    Can you help me out.

    thanks in advance

  18. manoj says:

    How can i select particular tray for printing pdf?

  19. danielle says:

    Hi – I just tried to use your solution, unfortunately it doesn’t work on Acrobat 8. I saw a message posting on the adobe site from a member of the adobe team, stating that there is a bug in Reader 8.0.

  20. Bijal says:

    hiiiiiii i want to print word and pdf file in vb.net. pdf file is print. but with word file its not working.
    Dim psi As New ProcessStartInfo
    psi.UseShellExecute = True
    psi.Verb = “print”
    psi.WindowStyle = ProcessWindowStyle.Hidden
    psi.FileName = sReport
    Process.Start(psi)
    with this code doc file is not print. its not get file from system.
    plz help me as soon as possible.
    thanks.

  21. Bijal says:

    with word document it gives this type of error.
    win32 excaption was cought.
    The system cannot find the file specified.

    plz help me.

  22. Ripal Soni says:

    Hi Bijal,

    did you try printing word file using the code given in this post ?

    in this post ?

    try Below code , that might help you , If not let me know

    Dim pathToExecutable As String = “word.exe” (It should be the name of your Microsoft word exe – you can find it from C:\programmefiles\Microsoftword folder )

    Dim sReport = “C:Test.doc” ‘Complete name/path of word document

    Dim SPrinter = “HP Officejet 5600 seriese” ‘Name Of printer

    Dim starter As New ProcessStartInfo(pathToExecutable, “/t “ + sReport + ” “ + sPrinter + “”)

    Dim Process As New Process()

    Process.StartInfo = starter Process.Start()
    ‘try and close the process with 20 seconds delay
    System.Threading.Thread.Sleep(30000)

    Process.CloseMainWindow()
    Dim iLoop As Int16 = 0
    ‘check the process has exited or not

    If Process.HasExited = False Then

    ‘if not then loop for 100 time to try and close the process‘with 10 seconds delay
    While Not Process.HasExited
    System.Threading.Thread.Sleep(10000)
    Process.CloseMainWindow()
    iLoop = CShort(iLoop + 1)

    If iLoop >= 100 Then

    Exit While
    End If
    End While

    End If

    Process.Close()
    Process.Dispose()
    Process = Nothing
    starter = Nothing

    • Chandra says:

      Hi Ripal
      Soni,
      My requirement is to print a word document to print on client system. Means I want to print a document opened by user using ajax editor (I am displaying the editor content as word document and I want to print that document). When I write code in C# it is working when I run on local system, but when I publish it is not working because it is trying to access the printer connected to web server.)
      Can you please tell me if use the C# script you provided will solve my pr0blem.

      Thanks in advance.
      Mouli.

  23. Bijal says:

    thanks for sending me code.its work on that code. i try first of all this code but i cant find whole path of word. then with the verb its done. its error in my word doc.its currupted. ok. thanks

  24. Jacky says:

    Process.StartInfo = starter Process.Start()
    I don’t know why I have unexpected end error occuring on this statement?

  25. StephenRay says:

    To all you that this won’t work on Acrobat 8.0. The solution that I found is in the command line arguments for acrobat. /t must be in the format
    (AcroRd32.exe /t path printername drivername portname) This worked for me. Found the information @ http://www.planetpdf.com/forumarchive/49365.asp

  26. StephenRay says:

    Just noticed that the document I referred to was written in 2002. However, until I added the drivername portname parameters to the command, it would not print. May have to do with printing to a shared printer, I don’t know. I’m just sharing the little information I have garnered :)

  27. Micca says:

    :( :( Still I cant print
    This is how my Application suppose to work

    I have a folder that I have to monitor , each time a new PDF is loaded I have to print it
    Please help
    Micca@cricketmail.net

  28. adam says:

    I think the part that a lot of people are having problem with are long paths that have spaces, when setting this up in VB you will need to include the quotes in the string. Here is what I got to work:

    Dim pathToExecutable As String = “AcroRd32.exe”
    Dim sReport = “C:\Dell\My review.pdf” ‘Complete name/path of PDF file
    Dim SPrinter = “Black Ice ColorPlus” ‘Name Of printer
    Dim starter As New ProcessStartInfo(pathToExecutable, “/t “”” + sReport + “”” “”” + SPrinter + “”””)
    Dim Process As New Process()
    Process.StartInfo = starter
    Process.Start()
    Process.WaitForExit(10000)
    Process.Kill()
    Process.Close()

    Hope this helps.
    p.s. this also kills the adobe instances so if you are setting this up as a service you won’t have several adobe windows up.

  29. Amit says:

    Using the above step, document opened in a pdf. But it doesn’t get printed. Please let me know how should it get printed also.

    Thanks,
    Amit

  30. Feejaz says:

    I have tried to use this code But I found the error on line
    “Process.Start()”
    that
    The system cannot find the file specified
    I have checked the file is at there place. Can you tell me why I am getting this error.

    Feejaz

  31. magy says:

    Hi Feejaz u must put @ symbol before the file path.

    psi.FileName = @”E:\magy.pdf”;

  32. Ganesh says:

    Dear Ripal Soni,
    Below is my code in C# for Printing PDF Files in network server. The file is always printing in default printer only, which is set in my local machine. It is not printing to the printer specified in the below code. Please let me know the solution for this.
    Thanks,

    String pathToExecutable = @”AcroRd32.exe” ;
    String s1Reports= @”D:\TestDocs\PrintScreenUI.pdf”;
    String SPrinter= @”\\ccrnt008\HPLaser2F”;
    ProcessStartInfo starter = new ProcessStartInfo(pathToExecutable, “/t ” + s1Reports + ” ” + SPrinter + ” “) ;
    Process ps = new Process();
    ps.StartInfo = starter;
    ps.Start();
    ps.WaitForExit(10000);
    ps.CloseMainWindow();
    ps.Close();

  33. dj says:

    Hi StephenRay,
    Could you post or email me your code.
    Thanks for the help in advance.

    DJ

  34. Roopa says:

    hi Ganesh,

    i have same problem
    from ASP.NET (Framework 1.1), webpage i wanna silently print a pdf(without opening PDF).
    the above code is working ( means it just opened the PDF file) in dot net 2005 but not in 2003.
    plzzz Help me

  35. Greg says:

    How can i get to work with Word…..i cannot supply a printer from the command line. With word, any other parameter you send just gets interpreted as another document. I don’t want to have the default printer as the PDF writer…..

    How can i bypass word, and just send a .doc file to the printer i want…

  36. PH-cio says:

    Can you specify the number of copies?
    Thanks.
    Your reply would greatly help and be greatly appreciated.

  37. Selva says:

    Hi,

    I have one question:

    When i open the pdf document i need to save the pdf document into some other format like .tiff. can u pl tell me now to do that…

    thanks in advance
    selvaraj

  38. sue says:

    This is what I have that runs without error and starts adobe but nothing prints. What am I missing?

    Dim pathToExecutable As String = adobeVer
    Dim SPrinter = printer ‘Name Of printer
    Dim SDriver = driver
    Dim SPortName = port
    Dim starter As New ProcessStartInfo(pathToExecutable, “/t “”” + PDFFile + “”” “”” + SPrinter + “”” “”” + SDriver + “”” “”” + SPortName + “”””)
    starter.Verb = “print”
    starter.UseShellExecute = False
    Dim Process As New Process()
    Process.StartInfo = starter
    Process.Start()

  39. rani says:

    hi ….

    i want to print the Html doc

    can u plz help

    • Krishna Reddy Ganta says:

      use webBrowser control to display the html content.
      and for printing use the below
      webBrowser1.ShowPrintPreviewDialog();

  40. Nice blog about printer. I have a lot of problem with my office printer. I’m IT support and need to solve any problem. Do you have any suggestion about printer troushooting guide site? Thank you very much.

  41. Venk says:

    This was exactly my requirement. I just went ahead to try this. But no luck!.

    When i work with the .Net (c# asp.net 3.5)(i converted the codes to c#) editor (visual studio 2008) it works perfectly.
    But when i publish the codes in a differnt machine OR even in the same machine as a virtual directory, it does not work.

    No errors, but does not print.

    Any solution? Please help.

    my acrobat reader installation is the usual path in c:\program files….. and printer is a network printer, pdf is available in the path menthioned in the code..

  42. Venk says:

    This was exactly my requirement. I just went ahead to try this. But no luck!.

    When i work with the .Net (c# asp.net 3.5)(i converted the codes to c#) editor (visual studio 2008) it works perfectly.
    But when i publish the codes in a differnt machine OR even in the same machine as a virtual directory, it does not work.

    No errors, but does not print.

    Any solution? Please help.

    my acrobat reader installation is the usual path in c:\program files….. and printer is a network printer, pdf is available in the path menthioned in the code..

  43. Wajahat Gilani says:

    Hello Ripal,

    Your code worked perefectly for me, but do you know how to make the code print on both sides of the paper?

    • ripalsoni says:

      for more info about duplex printing – http://www.vbdotnetforums.com/reporting-printing/25422-duplex-printing.html

      Option Strict On
      Option Explicit On

      Imports System.IO
      Imports System.Drawing.Printing
      Imports System
      Imports System.Drawing

      Public Class Form1

      Private TotalToPrint As Integer = 0
      Private iPageNumber As Integer = 0

      Private blnPrintPreview As Boolean = False
      Private pdPrinter As PrintDocument
      Private ppvwPrinter As PrintPreviewDialog

      Private Sub Button1_CLick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
      pdPrinter = New PrintDocument
      ppvwPrinter = New PrintPreviewDialog
      AddHandler pdPrinter.BeginPrint, AddressOf TestDuplex_BeginPrint
      AddHandler pdPrinter.PrintPage, AddressOf TestDuplex_PrintPage
      AddHandler pdPrinter.EndPrint, AddressOf TestDuplex_EndPrint

      If blnPrintPreview = True Then
      ppvwPrinter.Document = pdPrinter
      ppvwPrinter.Width = Screen.PrimaryScreen.Bounds.Width
      ppvwPrinter.Height = Screen.PrimaryScreen.Bounds.Height
      ppvwPrinter.PrintPreviewControl.Zoom = 0.5
      ppvwPrinter.ShowDialog()
      Else
      pdPrinter.PrintController = New StandardPrintController ‘silent printing
      pdPrinter.Print()
      End If

      RemoveHandler pdPrinter.BeginPrint, AddressOf TestDuplex_BeginPrint
      RemoveHandler pdPrinter.PrintPage, AddressOf TestDuplex_PrintPage
      RemoveHandler pdPrinter.EndPrint, AddressOf TestDuplex_EndPrint
      pdPrinter.Dispose()
      ppvwPrinter.Dispose()
      End Sub

      Private Sub TestDuplex_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
      pdPrinter.DocumentName = “Test Duplex”
      pdPrinter.OriginAtMargins = True
      pdPrinter.DefaultPageSettings.Landscape = True
      pdPrinter.PrinterSettings.Duplex = Printing.Duplex.Vertical
      pdPrinter.PrinterSettings.Copies = 2

      TotalToPrint = 3
      iPageNumber = 0
      End Sub

      Private Sub TestDuplex_PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs)
      iPageNumber += 1

      e.Graphics.PageUnit = GraphicsUnit.Millimeter

      e.Graphics.DrawString(“PAGE ” & iPageNumber.ToString, New Font(“Arial”, 8, FontStyle.Bold), Brushes.Black, 20, 20)

      If iPageNumber < TotalToPrint Then
      e.HasMorePages = True
      Else
      e.HasMorePages = False
      End If
      End Sub

      Private Sub TestDuplex_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
      'clean up here if necessary
      End Sub

      End Class

  44. Benjamin says:

    Hi Ripal,

    I’m so interested about your code to print pdf’s. Do you know generally how can we set up the printing configuration. For example, how can we control the printing style. If we have powerful printer which can help staple and punch printed materials,
    how can we used these functions? I would really appreciate it if you could help me out.

  45. marie says:

    nice codes..
    can you help me?
    how to print a pdf without giving the exact complete name of the pdf??
    tnx a lot….

  46. Taiff says:

    Im using vb.net 2008, The following line of code is not working for me.
    Process.StartInfo = starter Process.Start()

  47. Clement says:

    Guys i need help i want to code a button in vb.net 2 print so i dnt knw wer 2 starts help

  48. Gryfu says:

    Hi.
    I’ve tried this method and it doesn’t work.
    While opening Adobe it shows message:
    “An error ocurred. File can not be found”
    When I remove sPrinter from line 07
    Dim starter As New ProcessStartInfo(pathToExecutable, “/t ” + sReport + ” ” + sPrinter + “”)
    it finds file normally, but printer name is important and i have to send it to adobereader.

    Any suggestions?

  49. it training says:

    For most up-to-date news you have to pay a quick visit web and on internet I found this web site
    as a best web site for most recent updates.

  50. kamal says:

    the code work fine for me, but only work for default printer setting. for example, default printer tray is tray 1. but when i change the default tray to tray 2, the code cannot work.
    how to select the printer tray in the code since Dim starter As New ProcessStartInfo(pathToExecutable, “/t ” + sReport + ” ” + sPrinter + “”) function only get file name and printer name only..???

  51. Your posting, “Print PDF file in VB.Net by giving Printer Name .
    NET Interview Questions” ended up being very well worth commenting down here in the comment section!

    Basically desired to announce u really did a superb work.
    I appreciate it -Debra

  52. juno says:

    hello everyone, it looks like this code is not working. It keeps saying “The system cannot find the file specified” when in fact the file is there. This code is very buggy.

Trackbacks
Check out what others are saying...
  1. […] Print PDF file in VB.Net by giving Printer Name .Net 2.0 Interview Terms / Interview Questions […]



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: