2010-12-16 53 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) 
     { 
     } 
    }