-2

We have an requirement: There are certain PDF's which will be generated at Server Side. We need to make Client to take PrintOut of these files without opening them.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfPrinter
{
class Program
{
    static void Main(string[] args)
    {
        string[] files = Directory.GetFiles(@"d:\files");
        foreach (string file in files.Where(
                    file => file.ToUpper().Contains(".PDF")))
        {
            Pdf.PrintPDFs(file);
        }
    }
}//END Class

public class Pdf
{
    public static Boolean PrintPDFs(string pdfFileName)
    {
        try
        {
            Process proc = new Process();
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc.StartInfo.Verb = "print";

            //Define location of adobe reader/command line
            //switches to launch adobe in "print" mode
            proc.StartInfo.FileName =
              @"C:\Program Files (x86)\Adobe\Acrobat Reader    DC\Reader\AcroRd32.exe";
            proc.StartInfo.Arguments = String.Format(@"/p /h {0}", pdfFileName);
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;

            proc.Start();
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            if (proc.HasExited == false)
            {
                proc.WaitForExit(10000);
            }

            proc.EnableRaisingEvents = true;

            proc.Close();
            KillAdobe("AcroRd32");
            return true;
        }
        catch
        {
            return false;
        }
    }

    //For whatever reason, sometimes adobe likes to be a stage 5 clinger.
    //So here we kill it with fire.
    private static bool KillAdobe(string name)
    {
        foreach (Process clsProcess in Process.GetProcesses().Where(
                     clsProcess => clsProcess.ProcessName.StartsWith(name)))
        {
            clsProcess.Kill();
            return true;
        }
        return false;
    }
}//END Class
}//END Namespace

The above program is working fine from Developer Side, but Its not working on Client Side. I think I am unable to invoke Client's Adobe Reader.

Can anyone help me, how can I invoke client's AdobeReader executable file.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • Right, so you are saying that they are allowed to read it (on paper) and also not allowed to read it (on the screen). That's just silly. – JK. Sep 29 '15 at 04:43
  • @JK, its not good to criticize others requirements,actually our requirement is to not to enable the client to save the PDF on to his machine. – Siva Tarun Oct 05 '15 at 06:14

4 Answers4

0

Well any hope of helping you is lost because we don't know the error. Why do you eat the exception?

Change try { ... } catch { return false; } to

 try { ... }
 catch (Exception e)
 {
   Debug.WriteLine(e.Message);
   return false;
 }

Update

  1. Make sure the pdfFileName is an absolute path, e.g. "c:\foo\foo.pdf" not "foo.pdf"

  2. The path to the pdf file may contain spaces, so try

    proc.StartInfo.Arguments = String.Format("/p /h \"{0}\"", pdfFileName);

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
0

You cannot start an arbitrary executables on a client's machine from a webpage, for very obvious reasons.

Also, that code you show runs on the server. While developing, your development machine happens to be the server, which made you think it works.

There is no fool-proof, cross-browser, cross-platform way to properly print a document from a webpage without user intervention.

The only workable workaround would be to display the PDF in an iframe/embed and call window.print() on it from the parent frame. See Print PDF directly from JavaScript.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

It works on "developer side" because both the server and client refer to the same machine. It does not work on the "client side" because the printing is happening in different machines. It is getting printed on the server and the client who is on a different machine will never see it.

Check this answer for a print without preview option from a web page https://stackoverflow.com/a/30569808/1434413

Community
  • 1
  • 1
gn1
  • 526
  • 2
  • 5
-1

we can do this by using System.Web.UI namespace in C#

First add the below code to your .aspx

 <object id = "Object1" name="Pdf2" 
     type="application/pdf" width="1" height="1" >
    <param name='SRC' value='NameOfPDFfile'/>

then by using the below code .CS file

ClientScript.RegisterScript(typeof(Page),"MessagePopUp","<script language="+"JavaScript"+">document.Pdf2.printAll()></script>");

Please note that AdobeReader should be installed on Client Machine in order to work this code.

  • Wait so you did all of this just to self answer? Self answered questions are allowed, but the question is still required to meet the same quality standards as any other question. – JK. Sep 29 '15 at 04:41
  • And BTW this does not even work. The user can simply view the source code and copy the `src` parameter and then directly open the PDF. – JK. Sep 29 '15 at 04:42