/// <summary> /// 使用FixedPage模板并加载渲染数据 /// </summary> /// <param name="strTmplName"></param> /// <param name="data"></param> /// <returns></returns> public static FixedPage LoadFixedDocumentAndRender(string strTmplName, Object data) { FixedPage doc = (FixedPage)Application.LoadComponent(new Uri(strTmplName, UriKind.RelativeOrAbsolute)); if ("Letter".Equals(App.reportSettingModel.PrintPaper.ToString(), StringComparison.OrdinalIgnoreCase)) { doc.Width = 96 * 8.5; doc.Height = 96 * 11; } else if ("A4".Equals(App.reportSettingModel.PrintPaper.ToString(), StringComparison.OrdinalIgnoreCase)) { doc.Width = 96 * 8.27; doc.Height = 96 * 11.69; } doc.DataContext = data; //加载图片到文档中 if (data.GetType() == typeof(ShortFormReport)) { var shortFormReport = data as ShortFormReport; if (shortFormReport != null) { var topImage = doc.FindName("imgTitleLog") as Image; if (topImage != null) { if (!App.reportSettingModel.DefaultLogo) { topImage.Source = ImageTools.GetBitmapImage(AppDomain.CurrentDomain.BaseDirectory + "logo.png"); } } var signImage = doc.FindName("dataSignImg") as Image; if (signImage != null && shortFormReport.DataSignImg != null) { signImage.Source = ImageTools.GetBitmapImage(shortFormReport.DataSignImg); } var screenShotImg = doc.FindName("dataScreenShotImg") as Image; if (screenShotImg != null && shortFormReport.DataScreenShotImg != null) { screenShotImg.Source = ImageTools.GetBitmapImage(shortFormReport.DataScreenShotImg); } if (!App.reportSettingModel.ShowTechSignature) { var techSignPanel = doc.FindName("techSignPanel") as Panel; if (techSignPanel != null) { techSignPanel.Visibility = Visibility.Collapsed; } } if (!App.reportSettingModel.ShowDoctorSignature) { var doctorSignPanel = doc.FindName("doctorSignPanel") as Panel; if (doctorSignPanel != null) { doctorSignPanel.Visibility = Visibility.Collapsed; } var doctorSignGrid = doc.FindName("doctorSignGrid") as Panel; if (doctorSignGrid != null) { doctorSignGrid.Visibility = Visibility.Collapsed; } } } } return(doc); }
private void Print(string pageDirection, string printerName) { PrintDialog print = new PrintDialog(); var orderLableModels = _viewModel.AdviceLables; //打印队列 LocalPrintServer localPrintServer = new LocalPrintServer(); PrintQueueCollection printQueues = localPrintServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections }); if (printQueues == null || !printQueues.Any()) { MessageBox.Show("PrintQueues为空,打印出错!"); return; } //设置打印机信息 PrintQueue queue = printQueues.FirstOrDefault(p => p.Name == printerName); if (queue == null) { MessageBox.Show("获取打印机驱动失败!"); } print.PrintQueue = queue; int recordCount = _viewModel.AdviceLables.Count(); FixedPage orderLabelPrintPage = null; //找程序目录下,如果找不到就读程序预制的打印样式文件。 Uri printViewUrl = null; try { printViewUrl = new Uri("/WpfApp_OrderLabel_Print;component/OrderLabelPage_Zhfy.xaml", UriKind.RelativeOrAbsolute); orderLabelPrintPage = (FixedPage)Application.LoadComponent(printViewUrl); } catch (Exception ex) { MessageBox.Show(string.Format("加载打印资源错误,原因:{0},请联系系统管理员!", ex.Message)); return; } double height = orderLabelPrintPage.Height; double width = orderLabelPrintPage.Width; //打印纸张方向 if ("0".Equals(pageDirection)) { print.PrintTicket.PageOrientation = System.Printing.PageOrientation.Portrait; //横印 print.PrintTicket.PageMediaSize = new PageMediaSize(width, height); } else { print.PrintTicket.PageMediaSize = new PageMediaSize(height, width); print.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape; //横印 } #region 直接打印 FixedDocument fixedDocument = new FixedDocument(); PageContent pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(orderLabelPrintPage); fixedDocument.Pages.Add(pageContent); TextBlock TbAdvType = (TextBlock)orderLabelPrintPage.FindName("TbAdvType"); TextBlock tbBedNo = (TextBlock)orderLabelPrintPage.FindName("TbBedNo"); TextBlock tbPatName = (TextBlock)orderLabelPrintPage.FindName("TbPatName"); TextBlock tbPatSex = (TextBlock)orderLabelPrintPage.FindName("TbPatSex"); Image imgCode = (Image)orderLabelPrintPage.FindName("ImgCode"); TextBlock tbIpNo = (TextBlock)orderLabelPrintPage.FindName("TbIpNo"); TextBlock tbPatAge = (TextBlock)orderLabelPrintPage.FindName("TbPatAge"); TextBlock TbDeptName = (TextBlock)orderLabelPrintPage.FindName("TbDeptName"); TextBlock TbRePrint = (TextBlock)orderLabelPrintPage.FindName("TbRePrint"); StackPanel SpOrderList = (StackPanel)orderLabelPrintPage.FindName("SpOrderList"); //样式 Style ItemNameTextBlockStyle = (Style)orderLabelPrintPage.TryFindResource("ItemNameTextBlockStyle"); Style DosageTextBlockStyle = (Style)orderLabelPrintPage.TryFindResource("DosageTextBlockStyle"); //其他资源 int iMaxItemWordCount = (int)orderLabelPrintPage.TryFindResource("MaxItemWordCount"); int iMaxDosageWordCount = (int)orderLabelPrintPage.TryFindResource("MaxDosageWordCount"); TextBlock tbFreq = (TextBlock)orderLabelPrintPage.FindName("TbFreq"); TextBlock tbSeq = (TextBlock)orderLabelPrintPage.FindName("TbSeq"); TextBlock tbExecDate = (TextBlock)orderLabelPrintPage.FindName("TbExecDate"); TextBlock tbUsage = (TextBlock)orderLabelPrintPage.FindName("TbUsage"); for (int i = 0; i < recordCount; i++) { OrderLableInfo orderLable = orderLableModels[i]; #region 第一部分 TbAdvType.Text = orderLable.AdvListInfo.AdvTypeName; tbBedNo.Text = orderLable.AdvListInfo.BedNo; tbPatName.Text = orderLable.AdvListInfo.PatName; tbPatSex.Text = orderLable.AdvListInfo.PatSex; tbIpNo.Text = orderLable.AdvListInfo.IpNo; tbPatAge.Text = orderLable.AdvListInfo.PatAge; imgCode.Source = ConvertBytesToBitImage(orderLable.AdviceLabelBytes); TbDeptName.Text = orderLable.AdvListInfo.DeptName; TbRePrint.Visibility = orderLable.RePrint ? Visibility.Visible : Visibility.Hidden; #endregion #region 第二部分 SpOrderList.Children.Clear(); foreach (AdviceInfo advice in orderLable.AdvListInfos) { Tuple <bool, string, string, string, string> tuple = HandleOrderContent(advice, iMaxItemWordCount, iMaxDosageWordCount); if (tuple.Item1) { SpOrderList.Children.Add(GetWrapContentStackPanel(tuple.Item2, tuple.Item3, tuple.Item4, tuple.Item5, ItemNameTextBlockStyle, DosageTextBlockStyle)); } else { SpOrderList.Children.Add(GetSingleContentStackPanel(tuple.Item2, tuple.Item4, ItemNameTextBlockStyle, DosageTextBlockStyle)); } } #endregion #region 第三部分 tbFreq.Text = orderLable.AdvListInfo.FrequenName; tbSeq.Text = orderLable.AdvListInfo.Seq; tbExecDate.Text = orderLable.AdvListInfo.ExecTime == null ? "" : orderLable.AdvListInfo.ExecTime.Value.ToString("yyyy-MM-dd"); tbUsage.Text = orderLable.AdvListInfo.UsageName; #endregion //documentViewer.Document = fixedDocument; print.PrintDocument(fixedDocument.DocumentPaginator, "贴瓶单"); } #endregion }