【问题标题】:How can I use c# to set printersettings?如何使用 c# 设置打印机设置?
【发布时间】:2010-12-16 00:12:33
【问题描述】:

编辑 我试图重构我不再需要展示的代码。我认为这只是打印机设置类的一个限制,它没有公开可以通过使用对话框来选择的功能。看来我应该能够配置一个printerSettings对象并将其分配给一个PrintDocument,然后打印那个PrintDocument ...???我是不是在这里思考?

再次编辑 我认为所有的二传手都坐在'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 Env,文档 = 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

任何帮助表示赞赏。

谢谢

【问题讨论】:

    标签: c# winforms printing printers


    【解决方案1】:

    您或许可以使用 WMI。我唯一的 WMI 经验是一些用于 WMI 的 C# 代码来检索一些打印机属性,我没有尝试设置任何打印机属性,但我认为它应该是可能的。也许这些 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();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:
          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)
              {
              }
          }
      

      【讨论】:

      • 您为标签打印机提供代码非常有用(因为 99% 的时间我需要弄乱打印机代码是因为愚蠢的标签打印机)
      猜你喜欢
      • 2015-07-07
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      相关资源
      最近更新 更多