23

I'm programming a web application with Visual Studio 2010 (C#). I want to send a PDF (saved in my computer) to a printer when I click a button.

To create the PDF I used iTextSharp. I tried this, but it just opens Adobe Reader:

               proc.StartInfo.FileName = @"C:\Archivos de programa\Adobe\Reader10.0\Reader\AcroRd32.exe";
               proc.StartInfo.Arguments = String.Format(@"/p /h {0}", pdfFileName);
               proc.StartInfo.UseShellExecute = false;
               proc.StartInfo.CreateNoWindow = true;

               proc.Start();

Thank you in advance!!!

Alsan
  • 325
  • 1
  • 5
  • 12

1 Answers1

45

this has already been asked and answered here: How can I send a file document to the printer and have it print?

The code that was used:

private void SendToPrinter()
    {
        ProcessStartInfo info = new ProcessStartInfo();
        info.Verb = "print";
        info.FileName = @"c:\output.pdf";
        info.CreateNoWindow = true;
        info.WindowStyle = ProcessWindowStyle.Hidden;

        Process p = new Process();
        p.StartInfo = info;
        p.Start();

        p.WaitForInputIdle();
        System.Threading.Thread.Sleep(3000);
        if (false == p.CloseMainWindow())
            p.Kill();
    }

it basicly opens a "hidden" pdf-reader, tells it to print, waits for it to finish then close it down

Community
  • 1
  • 1
Johan Hjalmarsson
  • 3,433
  • 4
  • 39
  • 64
  • 4
    This is good solution, but what if I want to print document with manual printer setting? – Hakoo Desai Feb 14 '14 at 02:18
  • 4
    WaitForInputIdle() has no effect. It seems that p is in idle-Mode after Start(). Only the sleep for 3 seconds allows Adobe to finish the spooling. This might be a problem for large documents. – user1027167 Sep 12 '14 at 10:57
  • 2
    It doesn't work no more on Windows 8+ – Artem A Nov 10 '15 at 22:05
  • 1
    Although it is printing the file without any issue, but `p.WaitForInputIdle();` doesn't return any value and the program remain stuck on this line of code – sohaiby Aug 02 '17 at 17:00