2
Answers

Problem with printing PDFs from C# Forms application

Ask a question
Hi all
I have some code that will iterate through a list and print pdfs. On two pcs it works fine - one with Acrobat XI Pro, one with Acrobat reader 10. On another pc, also with Acrobat reader 10, no print jobs are spooled.
What could be wrong, and is there any way I can make it more robust and print correctly? In the code below, it doesn't go to the catch clause, so thinks it is printing correctly, and with no errors, so wonder if it is being killed before it has printed - but without this, the code prints one, then only continues if you manually close Adobe.

    public class Pdf
    {
        public Boolean PrintPDFs(string pdfFileName)
        {
            try
            {
                ProcessStartInfo info = new ProcessStartInfo();
                info.Verb = "print";
                info.FileName = pdfFileName;
                info.CreateNoWindow = true;
                info.WindowStyle = ProcessWindowStyle.Hidden;

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

                System.Threading.Thread.Sleep(10000);
                if (false == p.CloseMainWindow())
                {
                    KillAdobe("AcroRd32");
                }
                   
                return true;

            }
            catch (Exception ex)
            {
                File_operations logDetail = new File_operations();
                logDetail.WriteToLogFile(ex.Message);
                return false;
            }
        }
        private static bool KillAdobe(string name)
        {
            foreach (Process clsProcess in Process.GetProcesses().Where(
                         clsProcess => clsProcess.ProcessName.StartsWith(name)))
            {
                clsProcess.Kill();
                return true;
            }
            return false;
        }
    }

 

 


Answers (2)