Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.06/5 (3 votes)
See more:
I am getting the exception while trying to print a PDF silently in asp.net with C#. At times I am getting error opening the PDF. the process cannot find the file specified. Also I don't want the pdf to open up either.

Code is as below:
C#
private void print()
    {
        ProcessStartInfo infoPrintPdf = new ProcessStartInfo();
        string pathPdf = @"E:\eSAFE\FileStore\PDF\Account Statement_76876876878_040111_000018473018_20160620172958468.pdf";
        infoPrintPdf.FileName = pathPdf;
        // The printer name is hardcoded here, but normally I get this from a combobox with all printers
        string printerName = @"\\10.15.0.8\NTP Printer 001 SecondFloor";
        //string printerName = "HP LaserJet Professional P1606dn";
        string driverName = "hp1100sd.dll";
        string portName = "10.15.0.23";
        infoPrintPdf.FileName = @"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
        infoPrintPdf.Arguments = string.Format("/t {0} \"{1}\" \"{2}\" \"{3}\"",
            pathPdf, printerName, driverName, portName);
        infoPrintPdf.CreateNoWindow = true;
        infoPrintPdf.UseShellExecute = false;
        infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden;
        Process printPdf = new Process();
        printPdf.StartInfo = infoPrintPdf;
        printPdf.Start();

        // This time depends on the printer, but has to be long enough to
        // let the printer start printing
        System.Threading.Thread.Sleep(10000);

        if (!printPdf.CloseMainWindow())              // CloseMainWindow never seems to succeed
            printPdf.Kill(); printPdf.WaitForExit();  // Kill AcroRd32.exe

        printPdf.Close();  // Close the process and release resources

    }


What I have tried:

Code is as below:
C#
private void print()
    {
        ProcessStartInfo infoPrintPdf = new ProcessStartInfo();
        string pathPdf = @"E:\eSAFE\FileStore\PDF\Account Statement_76876876878_040111_000018473018_20160620172958468.pdf";
        infoPrintPdf.FileName = pathPdf;
        // The printer name is hardcoded here, but normally I get this from a combobox with all printers
        string printerName = @"\\10.15.0.8\NTP Printer 001 SecondFloor";
        //string printerName = "HP LaserJet Professional P1606dn";
        string driverName = "hp1100sd.dll";
        string portName = "10.15.0.23";
        infoPrintPdf.FileName = @"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
        infoPrintPdf.Arguments = string.Format("/t {0} \"{1}\" \"{2}\" \"{3}\"",
            pathPdf, printerName, driverName, portName);
        infoPrintPdf.CreateNoWindow = true;
        infoPrintPdf.UseShellExecute = false;
        infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden;
        Process printPdf = new Process();
        printPdf.StartInfo = infoPrintPdf;
        printPdf.Start();

        // This time depends on the printer, but has to be long enough to
        // let the printer start printing
        System.Threading.Thread.Sleep(10000);

        if (!printPdf.CloseMainWindow())              // CloseMainWindow never seems to succeed
            printPdf.Kill(); printPdf.WaitForExit();  // Kill AcroRd32.exe

        printPdf.Close();  // Close the process and release resources

    }
Posted
Updated 20-Jun-16 4:07am
v2
Comments
Either the file is not present in that directory or the program does not have sufficient permission to access the file.
Sergey Alexandrovich Kryukov 20-Jun-16 9:40am    
...and more importantly, to access the printer (which may or may not even exist).
Please see Solution 1.
—SA
Just realized that it is tagged with ASP.NET. :P
Ema112 20-Jun-16 8:55am    
The issue is happening when the length of filename is higher. I mean if the pdf filename is xyz.pdf (no issues) but if say Account Statement_dsdsd_1.pdf issue occurs.

You cannot do anything silently in a Web application, by pretty obvious reasons. Imagine for a second that you find some trick to use client's printer silently, which, first of all, means using a printer (which may or may not even exist) without the user's consent; and then imagine that the user figured out why it happens. I doubt the user would use your site again.

Set aside Web, even in a desktop application, nothing should print silently: 1) printer belongs to the user, not to your application; 2) printing should always be a rare event; as computing paves its way to the society, people print less and save paper and toner; deliberately forcing the user to print is a kind of crime.

Moreover, PDF is not a Web content; it is not a part of W3 standard. Don't be confused by the fact that browser's often show PDF as a Web page; this is all non-standard feature. Therefore, for PDF, the best strategy is simple provide a PDF file in a usual HTML anchor. The user knows better what to do with it. Printing of that resource is the work for a PDF software, which you don't have and don't control; only the user manages this stuff.

—SA
 
Share this answer
 
Comments
rahul234 20-Jun-16 9:43am    
Ok.Print is coming fine with no spacing in between the Filename of the PDF File.Is it possible to keep the adobe reader pop up coming up on starting the print process.
Sergey Alexandrovich Kryukov 20-Jun-16 10:32am    
No, it does not.
See also Solution 2. It makes no sense anyway, regardless of file size of spacing...
—SA
You seem to be missing a concept. This works and prints fine on YOUR DEVELOPMENT MACHINE. When you deploy this web application to a production web server, it will NOT work as ASP.NET code runs entirely server-side, NEVER on the client, and will NOT have access to any printers attached to the client machine.

Unless you have a printer connected to the web server (I seriously doubt it and recommend against doing such a thing), you cannot print anything using this code.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Jun-16 10:32am    
5!—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900