簡體   English   中英

進程始終按默認打印機打印文檔

[英]process always prints document by default printer

我在選擇打印機打印文檔時遇到問題。

我的代碼是:

var filename = @"C:\Users\I\Desktop\test.doc";

PrintDialog pd = new PrintDialog();

pd.PrinterSettings =new PrinterSettings();

    if (DialogResult.OK == pd.ShowDialog(this))
    {
        Process objP = new Process();

        objP.StartInfo.FileName = filename;


        objP.StartInfo.WindowStyle =

        ProcessWindowStyle.Hidden; //Hide the window. 

        objP.StartInfo.Verb ="print";

          objP.StartInfo.Arguments ="/p /h \"" + filename + "\" \"" + pd.PrinterSettings.PrinterName + "\"";
        objP.StartInfo.CreateNoWindow = false;
        //true;//!! Don't create a Window. 
        objP.Start();
        //!! Start the process !!// 
        objP.CloseMainWindow();
    }

無論我選擇什么, process總是會使用默認打印機,無論pd.PrinterSettings.PrinterName是什么值。

我的代碼出了什么問題?

您可能希望使用“PrintTo”而不是“print”來表示動詞。 您已經將objP.FileName設置為文件名,因此不需要在參數中復雜化。 在那里單獨傳遞打印機名稱。

var filename = @"C:\Users\I\Desktop\test.doc"; 

PrintDialog pd = new PrintDialog(); 

pd.PrinterSettings =new PrinterSettings(); 

if (DialogResult.OK == pd.ShowDialog(this)) 
{ 
    Process objP = new Process(); 

    objP.StartInfo.FileName = filename;
    objP.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //Hide the window.
    objP.StartInfo.Verb ="PrintTo";
    objP.StartInfo.Arguments = pd.PrinterSettings.PrinterName;
    objP.StartInfo.CreateNoWindow = false; 
    //true;//!! Don't create a Window.  

    objP.Start(); 
    //!! Start the process !!//  

    objP.CloseMainWindow(); 
}

嘗試更改pd.PrinterSettings =new PrinterSettings(); 閱讀這樣的東西:

pd.PrinterSettings =new System.Drawing.Printing.PrinterSettings; 

默認情況下,當您創建打印機設置實例時,它返回默認的打印機名稱只是一個fyi ...然后您可以嘗試這樣的事情

//sudu code
foreach(string strPrinter in PrinterSettings.InstalledPrinters)
{
 // or unless you know the name of the printer then skip this and assign it to the code above
}

暫無
暫無

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

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