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

时光怂恿深爱的人放手 提交于 2019-12-19 03:26:07

问题


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.


回答1:


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);



回答2:


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.




回答3:


Maybe it is helpful for you to use the "/n" parameter of Adobe Reader. At least your program keeps the focus. But one instance of the reader stays open.

AcroRd32.exe /n /t ...

See: Questions 619158



来源:https://stackoverflow.com/questions/5508763/c-sharp-prevent-adobe-reader-window-from-coming-up-when-trying-to-print-a-docume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!