0

I've been developing an application in visual c# which needs to print an existing pdf file at the press of a button.

I've been using the System.Diagnostics.Process method to print but it fails to work as desired in a Windows 8 environment as the "Printto" command doesn't seem to work there.

I would like to use an alternative such as possibly the System.Drawing.Printing.PrintDocument which had an example that works with text files but when I tried with a pdf it was printing random characters instead.

My google searches for such seem to come up empty or I'm not entering the right keywords, but I need a solution which not only prints pdf to target printer but can determine if the printer is ready, offline or out of paper as well as an error catch.

Please advise as I'm also willing to look at any SDK or third party routes recommendable.

edit: added code snippet I'm currently using:

        string defFile = (Path.Combine(GlobalVars.pdfPath, tkt_no + "_DEF.pdf"));
        string rwPrinter = "";
        if (GlobalVars.useDefaultPrinter == false) 
        { 
            foreach (string strPrinter in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                if (strPrinter.StartsWith("ZDesigner"))
                {
                    rwPrinter = strPrinter;
                    break;
                }
            }
        }
        if (jobdo.Equals("print"))
        {
            Process process = new Process();
            //process.StartInfo.CreateNoWindow = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.FileName = defFile;
            if (rwPrinter.Length > 0)
            {
                process.StartInfo.Verb = "printto";
                //process.StartInfo.Verb = (Path.Combine(System.Windows.Forms.Application.StartupPath, "printto.exe"));
                process.StartInfo.Arguments = "\"" + rwPrinter + "\"";
            }
            else
            {
                process.StartInfo.Verb = "print";
            }

            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            process.WaitForInputIdle();
            //while (process.MainWindowHandle == IntPtr.Zero)
            //{
            //    Thread.Sleep(20);
            //    process.Refresh();
            //}
            Thread.Sleep(7000);
            try
            {
                process.Kill();
            }
            catch { }
            // close any occurrences of Adobe Reader that may not close through a citrix environment regardless
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
            {
                if (p.ProcessName.Equals("AcroRd32"))
                {
                    ObjectQuery sq = new ObjectQuery
                        ("Select * from Win32_Process where ProcessID = '" + p.Id + "'");
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq);
                    foreach (ManagementObject oReturn in searcher.Get())
                    {
                        string[] o = new string[2];
                        oReturn.InvokeMethod("GetOwner", (object[])o);
                        if(o[0] != null)
                            if(o[0].Equals(System.Environment.UserName))
                                p.Kill();
                    }

                }
            }

            if (rwPrinter == "") 
            {
                rwPrinter = "the default printer";
            }
            else
                MessageBox.Show("Ticket " + tkt_no + " was printed to " + rwPrinter + ".");
        }
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
jfalberg
  • 141
  • 4
  • 16
  • http://stackoverflow.com/questions/17448465/send-pdf-file-to-a-printer-print-pdf – Tim Ebenezer Oct 31 '13 at 13:15
  • Can you show the code you are using? – Tony Oct 31 '13 at 13:15
  • added code snippet to my question, seems a bit messed up though. – jfalberg Oct 31 '13 at 13:41
  • Are there any solutions that avoid using the System.Diagnostics.Process method and can tell me the printer status as well? – jfalberg Oct 31 '13 at 13:46
  • If you control the machines I would recommend just installing Adobe Reader or FoxIt or something that registers and handles the `printto` verb for you – Chris Haas Oct 31 '13 at 13:54
  • As far as "Registers and handles the printto verb", I'm not sure I understood how as it looks like a regedit thing. My google search came up with a link from Microsoft which looked confusing to me. – jfalberg Oct 31 '13 at 14:39

1 Answers1

-2

Please see this, using iTextSharp library you can easily create PDF file: http://sourceforge.net/projects/itextsharp/

Ovais Khatri
  • 3,201
  • 16
  • 14