2
Answers

Problem with printing PDFs from C# Forms application

Photo of James Hooper

James Hooper

9y
428
1
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)

0
Photo of Zakir Han
NA 2 0 9y
James,
I think you're not asking about generating PDF, but already have PDF files you need to print. Using an external process like you're doing always carries the risk of something going wrong outside your code in the external process. It could be related to the version of Adobe reader itself. Or the PDF file that you are printing.
And since you're invoking an external process, you have no control over error handling.

A better way is to load the PDF file in your own program and print it directly from your application without the need of opening other process. Since .NET itself doesn't support PDF, you will have to use a component that supports loading PDF. Adobe itself sells an SDK for PDF, which is discussed in this article:
http://www.c-sharpcorner.com/UploadFile/hirendra_singh/how-to-show-pdf-file-in-C-Sharp/

Another good alternative is the LEADTOOLS SDK, which supports other formats as well, not only PDF. There's a PDF-printing sample project here:
http://support.leadtools.com/SupportPortal/CS/forums/44459/ShowPost.aspx
0
Photo of Raja T
NA 7.4k 6k 9y