0

I searched Google and found couple of solution. I used the following code. It can print to my network printer when application runs through Visual Studio with IIS Express but when I hosted application to my local host the following code does not print any thing.

ProcessStartInfo psi = new ProcessStartInfo(pdfFileName) { Verb = "Print" };
            psi.CreateNoWindow = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            Process p = new Process();
            p.StartInfo = psi;
            Process.Start(psi);
            System.Threading.Thread.Sleep(5000);
            if (false == p.CloseMainWindow())
            {
                p.Kill();
            }
            else
            {
                p.WaitForInputIdle();
            }

I am totally stuck. Please give me suggestion or give me either sample code or links.

Morshed
  • 165
  • 3
  • 16

1 Answers1

0

If I'm understanding this correctly, you're starting a new process with the filename of the PDF, and trying to pass a verb into the process so it knows what to do (to print the PDF).

I would comment out the "CreateNoWindow" part and see what happens. Do you have anything on the server that is capable of opening and displaying PDF files? PDF's could be handled by Edge, Adobe Reader, Reader application (on 8.1), IrfanView, etc etc, and those applications will no doubt handle the "Print" verb differently.

Your sleep function will also produce inconsistent results because it is waiting for a specific amount of time. You might have better luck with something like WinWaitActive from the AutoItX toolkit (as you are kind-of doing UI automation).

Also, instead of doing

if (false == p.CloseMainWindow())

you can do

if (!p.CloseMainWindow())

This can improved code readability (The ! is essentially the same as "not", so its checking if that returned value is Not True).

EDIT: Other considerations is that IIS Express would run under your local account which would no doubt have the printers installed. The full IIS server would likely run as the Network Service account which may not have any printers installed.

You might consider it a potentially wiser design decision to maybe do something like, say, have your PDF's generated into a folder, and then have another C# application run on the server as a service to print all files in this folder as they come in. You could run that service as a user who has those printers installed. You could implement a FileSystemWatcher to only trigger when new files are put into that directory, handle the print job, and then delete the PDF. See here for how that all works (the file watching bit of it): Using FileSystemWatcher to monitor a directory

Community
  • 1
  • 1
Lewis Cianci
  • 926
  • 1
  • 13
  • 38
  • Thanks Lewis for your comments. I am not clear something from your comments. Do you have anything on the server that is capable of opening and displaying PDF files? – Morshed Dec 23 '16 at 00:33
  • Thanks Lewis for your comments. I commented out "CreateNoWindow" but app cannot print from server side. I am not clear something from your comments. 1. Do you have anything on the server that is capable of opening and displaying PDF files? 2. another C# application run on the server as a service to print all files in this folder as they come in. In your 2nd comment what kind of service? Again, same question raise how service will send print command from server side? I generated a PDF file into a folder and tried to print from that folder. – Morshed Dec 23 '16 at 00:54
  • I have found another code but still it can not print pdf file. code as follows. System.Diagnostics.Process.Start(@"C:\\Programs\Adobe\Reader 9.0\Reader\AcroRd32.exe", @"/t C:\test.pdf \\Server\Printer"); . Though, in this code, specified acrobat reader, pdf file and printer name, but it can not print pdf file. I have found another link https://support.microsoft.com/en-us/kb/322091. it can not print from IIS Express or Local IIS. I need solution immediately. Any suggestion,idea or a sample code? – Morshed Dec 23 '16 at 01:20