1、业务需求
工作室有多个报表需要打印,如果在报表模板里设置默认打印机的话,每个人电脑上安装的打印机是不相同的,所以就需要设定各自的默认打印机实现打印功能。
2、xml模板设计(PrinterSetting.xml)
1 <?xml version="1.0" encoding="utf-8"?> 2 <root> 3 <!--打印设置--> 4 <printerSettings> 5 <!--报表名称--> 6 <setting ReportName="检测条码" PrinterName="OneNote for Windows 10" /> 7 <setting ReportName="申请单条码" PrinterName="Microsoft Print to PDF" /> 8 </printerSettings> 9 </root>
2.9 加载打印机列表
1 private void InitReportPrinter() 2 { 3 var printers = PrinterSettings.InstalledPrinters;//获取电脑上安装的所有打印机 4 foreach (var printer in printers) 5 { 6 cmdReportType.Items.Add(printer.ToString()); 7 } 8 cmdReportType.SelectedIndex = 0; 9 var reportPrinterName = ReportService.GetDefaultReportPrinter(reportName); 10 if (!string.IsNullOrEmpty(reportPrinterName)) 11 { 12 cmdReportType.SelectedItem = reportPrinterName; 13 } 14 }
3、进入报表预览界面获取已经设置好的默认打印机
/// <summary> /// 根据文件名获取打印机 /// </summary> /// <param name="reportName"></param> /// <returns></returns> public static string GetDefaultReportPrinter(string reportName) { string reportPrinterName = ""; XmlDocument doc = new XmlDocument(); string newPath = GetPath();//在最后 doc.Load(newPath); XmlNode root = doc.SelectSingleNode($"root/printerSettings/setting[@ReportName='{reportName}']"); if (root != null) { var ele = (XmlElement)root; reportPrinterName = ele.GetAttribute("PrinterName"); } return reportPrinterName; }
4、设定打印机
/// <summary> /// 指定文件对应的打印机 /// </summary> /// <param name="reportName"></param> /// <param name="reportPrinterName"></param> /// <returns></returns> public static bool SetDefaultReportPrinter(string reportName, string reportPrinterName) { XmlDocument doc = new XmlDocument(); string newPath = GetPath(); doc.Load(newPath); XmlNode root = doc.SelectSingleNode($"root/printerSettings/setting[@ReportName='{reportName}']"); if (root != null) { var ele = (XmlElement)root; ele.SetAttribute("PrinterName", reportPrinterName); } else { XmlNode newXml = doc.SelectSingleNode(@"root/printerSettings"); XmlElement xmlreport = doc.CreateElement("setting"); xmlreport.SetAttribute("ReportName", reportName); xmlreport.SetAttribute("PrinterName", reportPrinterName); newXml.AppendChild(xmlreport); } doc.Save(newPath); return true; } /// <summary> /// 获取xml路径,不存在则创建 /// </summary> /// <returns></returns> private static string GetPath() { string path = Path.Combine(AppContext.BaseDirectory, "Config"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var dataFilePath = Path.Combine(path, "PrinterSetting.xml"); if (!File.Exists(dataFilePath)) { XmlDocument xmlDoc = new XmlDocument(); XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", ""); xmlDoc.AppendChild(node); XmlNode root = xmlDoc.CreateElement("root"); xmlDoc.AppendChild(root); XmlElement xmlreport = xmlDoc.CreateElement("printerSettings"); root.AppendChild(xmlreport); xmlDoc.Save(dataFilePath); } return dataFilePath; }
5、打印机设定
1 var printer = ReportService.GetDefaultReportPrinter(input.ReportName);//获取设定好的打印机名称 2 if (!string.IsNullOrEmpty(printer)) 3 { 4 report.PrintSettings.Printer = printer;//赋值 5 }
转自:http://Www.CnBlogs.Com/WebEnh/
如果想下次快速找到我,记得点下面的关注哦!
本博客Android APP 下载 |
![]() |
支持我们就给我们点打赏 |
![]() |
支付宝打赏 支付宝扫一扫二维码 |
![]() |
微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!