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 }