2010-12-16 92 views
2

編輯 我試圖重建我不再需要顯示的代碼。我認爲這僅僅是打印類設置的一個限制,不會暴露可以通過使用對話框選擇的功能。看來我應該能夠配置和分配一個printerSettings對象到PrintDocument,然後打印PrintDocument ...?我不是在想這裏或?如何使用c#設置打印機設置?

編輯再次 我認爲所有的setter坐在'printerSettings.DefaultPageSettings'。這將允許我修改打印機設置。我還沒有證明文件,不過稍後會

PrintDocument pd = new PrintDocument(); 
pd.DocumentName = "test.doc"; 

PrinterSettings printerSettings = new PrinterSettings(); 
printerSettings.?? <- I want to set the printer setting here e.g. DL, A4, etc 
pd.PrinterSettings = printerSettings; 
pd.Print(); 

我產生在C#(支票,信件,文件)Word郵件合併文件,但所有的這些都需要不同的打印機設置(查看=自定義設置,字母= DL信封,文件= A4)

我保存這些設置並加載打印機首選項對話框時,可以訪問他們,但我希望能夠將其建設成代碼,而不是手動更改打印機設置。我環顧四周,似乎打印機設置類應該是它,但我似乎無法得到它的工作。什麼我試圖做

//create the mail merge 
IList<Letter> letters = MailMerge.Create(enum.letters) 
Printer.Print(letters) //<-- in here I am trying set the printing preferences to DL Env 


//create the mail merge 
IList<Document> docs = MailMerge.Create(enum.documents) 
Printer.Print(docs) //<-- in here I am trying set the printing preferences to A4 

讚賞任何幫助

例如僞代碼。

謝謝

回答

3

您可能可以使用WMI。我唯一的WMI體驗是一些C#-code的WMI檢索某些打印機的屬性,我還沒有試過設置任何打印機的屬性,但我認爲它應該是可能的。也許這些MSDN鏈接和代碼可以幫助你開始。

WMI Tasks: Printers and Printing顯示了VB腳本的命令。 How To: Retrieve Collections of Managed Objects顯示如何使用SelectQuery和枚舉。 How To: Execute a Method顯示瞭如何執行一個方法:-)。

編輯:我剛剛注意到這個StackOverflow article: How do I programatically change printer settings ...,這似乎使用WMI來更改某些打印機設置。

我檢索代碼看起來是這樣的:

//using System.Management; 

    private void GetPrinterProperties(object sender, EventArgs e) 
    { 
     // SelectQuery from: 
     // http://msdn.microsoft.com/en-us/library/ms257359.aspx 
     // Build a query for enumeration of instances 
     var query = new SelectQuery("Win32_Printer"); 
     // instantiate an object searcher 
     var searcher = new ManagementObjectSearcher(query); 
     // retrieve the collection of objects and loop through it 
     foreach (ManagementObject lPrinterObject in searcher.Get()) 
     { 
      string lProps = GetWmiPrinterProperties(lPrinterObject); 
      // some logging, tracing or breakpoint here... 
     } 
    } 

    // log PrinterProperties for test-purposes 
    private string GetWmiPrinterProperties(ManagementObject printerObject) 
    { 
     // Win32_Printer properties from: 
     // http://msdn.microsoft.com/en-us/library/aa394363%28v=VS.85%29.aspx 
     return String.Join(",", new string[] { 
       GetWmiPropertyString(printerObject, "Caption"), 
       GetWmiPropertyString(printerObject, "Name"), 
       GetWmiPropertyString(printerObject, "DeviceID"), 
       GetWmiPropertyString(printerObject, "PNPDeviceID"), 
       GetWmiPropertyString(printerObject, "DriverName"), 
       GetWmiPropertyString(printerObject, "Portname"), 
       GetWmiPropertyString(printerObject, "CurrentPaperType"), 
       GetWmiPropertyString(printerObject, "PrinterState"), 
       GetWmiPropertyString(printerObject, "PrinterStatus"), 
       GetWmiPropertyString(printerObject, "Location"), 
       GetWmiPropertyString(printerObject, "Description"), 
       GetWmiPropertyString(printerObject, "Comment"), 
      }); 
    } 

    private string GetWmiPropertyString(ManagementObject mgmtObject, string propertyName) 
    { 
     if (mgmtObject[propertyName] == null) 
     { 
      return "<no "+ propertyName + ">"; 
     } 
     else 
     { 
      return mgmtObject[propertyName].ToString(); 
     } 
    } 
} 
0
private void startPrintingButton_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog ofd = new OpenFileDialog(); 
     if (DialogResult.OK == ofd.ShowDialog(this)) 
     { 
      PrintDocument pdoc = new PrintDocument(); 

      pdoc.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GK420d"; 
      pdoc.DefaultPageSettings.Landscape = true; 
      pdoc.DefaultPageSettings.PaperSize.Height = 140; 
      pdoc.DefaultPageSettings.PaperSize.Width = 104; 

      Print(pdoc.PrinterSettings.PrinterName, ofd.FileName); 
     } 
    } 

    private void Print(string printerName, string fileName) 
    { 
     try 
     { 
      ProcessStartInfo gsProcessInfo; 
      Process gsProcess; 

      gsProcessInfo = new ProcessStartInfo(); 
      gsProcessInfo.Verb = "PrintTo"; 
      gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden; 
      gsProcessInfo.FileName = fileName; 
      gsProcessInfo.Arguments = "\"" + printerName + "\""; 
      gsProcess = Process.Start(gsProcessInfo); 
      if (gsProcess.HasExited == false) 
      { 
       gsProcess.Kill(); 
      } 
      gsProcess.EnableRaisingEvents = true; 

      gsProcess.Close(); 
     } 
     catch (Exception) 
     { 
     } 
    }