簡體   English   中英

C#在嘗試打印文檔時防止出現 Adob​​e Reader 窗口

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

由於我現在無法進入的原因,我需要防止在我嘗試打印文檔時打開 Adob​​e Reader 窗口。 在我之前從事此工作的開發人員設置了以下標志,盡管我不確定它們的用途 -

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

任何幫助深表感謝!

謝謝,
泰迦。

雖然我無法具體回答您的問題,但我發現我無法做到這一點,因為我認為 Adob​​e 在版本 9 或 10 中更改了 Reader,因此您無法抑制打印對話框,並且窗口本身仍然不斷出現,而且由於我的用戶都安裝了不同版本的 Reader,我無法讓任何東西始終如一地工作。 如果您想嘗試查看 Reader's API - 您需要添加對正確 COM 庫的引用並從那里開始。 痛苦。

我最終通過GhostScript運行 PDF 完全放棄了 Adob​​e。 以下是我為完成這項工作而創建的助手類。 gsExePath應該類似於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)
        });
    }
}

以下應打印到 Windows 默認打印機:

var printerName = new System.Drawing.Printing.PrinterSettings().PrinterName;
var gs = new GSInterface(gsExePath);
gs.Print(filename, printername);

這可能僅適用於我工作的計算機,或者更廣泛地說,適用於安裝了 Windows 7 的 PC 上的此版本 Adob​​e (10),但每次打印到 .通過執行以下操作在任何其他應用程序中生成 pdf:

控制面板 >(設備和)打印機 > 雙擊“Adobe PDF”> 單擊“打印機”>“打印首選項”> 在“Adobe PDF 設置”選項卡中取消選中“查看 Adob​​e PDF 結果”。

關於 Adam 提出的解決方案(從控制面板關閉查看 Adob​​e PDF 結果)和 Serge Belov 評論(關於如何以編程方式進行),在注冊表中,此更改會影響 4 個位置的 ViewPrintOutput 值:

計算機\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\Adobe PDF\PrinterDriverData

計算機\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion\Print\Printers\Adobe PDF\PrinterDriverData

計算機\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Printers\Adobe PDF\PrinterDriverData

計算機\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\Adobe PDF\PrinterDriverData

但是,如果您從注冊表中更改它,它不會影響 adobe pdf 的行為,它仍然會在打印后顯示生成的 pdf

這可能會有所幫助,盡管https://groups.google.com/g/microsoft.public.access.reports/c/LWphtEVp6UI

也許使用 Adob​​e Reader 的“/n”參數對您有幫助。 至少你的程序保持焦點。 但是讀者的一個實例保持打開狀態。

AcroRd32.exe /n /t ...

請參閱: 問題 619158

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM