Posted by: ripalmsoni | April 25, 2007

Print PDF file in VB.Net by giving Printer Name

Here is the solution of printing PDF file by giving printer name

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

Any questions or suggestion regarding to this feel free to write.
Enjoy it. 8)

Responses

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

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.

Hello Ripal Soni,

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

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

too good :)

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?

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

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!

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.

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

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!

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

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

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

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

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

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

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

How can i select particular tray for printing pdf?

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.

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.

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

plz help me.

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

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

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

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

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 :)

:( :( 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

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.

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

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

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

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

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();

Leave a response

Your response:

Categories