Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Prevent Adobe Reader Window from coming up when trying to print a document

For reasons I can't get into right now, I need to prevent the Adobe Reader window from opening up when I try to print a document. The developer that was working on this before me has the following flags set, although I'm not really sure what they're for -

if (RegistryManager.GetAcrobatVersion() >= 9.0f)
    printerArg = "\"" + printerName + "\"";
else
    printerArg = printerName;

Process myProc = new Process();
myProc.StartInfo.FileName = fileName;
myProc.StartInfo.Verb = "printto";
myProc.StartInfo.UseShellExecute = true;
myProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProc.StartInfo.CreateNoWindow = true;
myProc.StartInfo.Arguments = "\"" + printerName + "\"";


bool result = myProc.Start();


if (myProc.WaitForInputIdle())
{
    if (!myProc.HasExited)
    {
        myProc.WaitForExit(Convert.ToInt32(5000));
        myProc.Kill();
    }
}
myProc.Close();

Any help is much appreciated!

Thanks,
Teja.

like image 288
Tejaswi Yerukalapudi Avatar asked Apr 01 '11 03:04

Tejaswi Yerukalapudi


2 Answers

This may apply only to the computers where I work, or, more broadly, to this version of Adobe (10) on PCs with Windows 7 installed, but I was able to suppress the opening of Acrobat (Pro) each time I printed to .pdf in any other application by doing the following:

Control Panel > (Devices and) Printers > Double click 'Adobe PDF'> Click 'Printer' > 'Printing Preferences' > Uncheck "View Adobe PDF Results" in the 'Adobe PDF Settings' Tab.

like image 40
Adam Avatar answered Oct 03 '22 14:10

Adam


While I can't answer your question specifically, I found that I couldn't do this as Adobe changed Reader I think at version 9 or 10 so that you couldn't supress the print dialog, and the window itself kept coming up anyway, and since my users all had different versions of Reader installed I couldn't get anything consistently working. If you want to try anyway have a look at Reader's API - you need to add a reference to the correct COM library and go from there. Painful.

I ended up dropping Adobe completely by running the PDF through GhostScript. Following is the helper class I created to do the job. gsExePath should be something like C:\Program Files\gs\gs8.71\bin\gswin32c.exe.

public class GSInterface
{
    public string GhostScriptExePath { get; private set; }

    public GSInterface(string gsExePath)
    {
        this.GhostScriptExePath = gsExePath;
    }

    public virtual void CallGhostScript(string[] args)
    {
        var p = new Process();
        p.StartInfo.FileName = this.GhostScriptExePath;
        p.StartInfo.Arguments = string.Join(" ", args);
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        p.Start();
        p.WaitForExit();
    }


    public void Print(string filename, string printerName)
    {
        this.CallGhostScript(new string[] {
            "-q",
            "-sDEVICE=mswinpr2",
            "-sPAPERSIZE=a4",
            "-dNOPAUSE",
            "-dNoCancel",
            "-dBATCH",
            "-dDuplex",
            string.Format(@"-sOutputFile=""\\spool\{0}""", printerName),
            string.Format(@"""{0}""", filename)
        });
    }
}

The following should print to the Windows default printer:

var printerName = new System.Drawing.Printing.PrinterSettings().PrinterName;
var gs = new GSInterface(gsExePath);
gs.Print(filename, printername);
like image 140
Rebecca Scott Avatar answered Oct 03 '22 14:10

Rebecca Scott