private static PrintQueue GetMicrosoftPdfPrintQueue() { PrintQueue pdfPrintQueue = null; try { using (var printServer = new PrintServer()) { var flags = new[] { EnumeratedPrintQueueTypes.Local }; // FirstOrDefault because it's possible for there to be multiple PDF printers with the same driver name (though unusual) // To get a specific printer, search by FullName property instead (note that in Windows, queue name can be changed) pdfPrintQueue = printServer.GetPrintQueues(flags) .FirstOrDefault(lq => lq.QueueDriver.Name == PdfPrinterDriveName); } if (pdfPrintQueue == null) { throw new Exception($"Could not find printer with driver name: {PdfPrinterDriveName}"); } if (!pdfPrintQueue.IsXpsDevice) { throw new Exception( $"PrintQueue '{pdfPrintQueue.Name}' does not understand XPS page description language."); } return(pdfPrintQueue); } catch { pdfPrintQueue?.Dispose(); throw; } }
private static PrintQueue GetMicrosoftPdfPrintQueue() { PrintQueue pdfPrintQueue = null; try { using (var printServer = new PrintServer()) { var flags = new[] { EnumeratedPrintQueueTypes.Local }; pdfPrintQueue = printServer.GetPrintQueues(flags).SingleOrDefault(lq => lq.QueueDriver.Name == PdfPrinterDriveName); } if (pdfPrintQueue == null) { throw new Exception($"Could not find printer with driver name: {PdfPrinterDriveName}"); } if (!pdfPrintQueue.IsXpsDevice) { throw new Exception($"PrintQueue '{pdfPrintQueue.Name}' does not understand XPS page description language."); } return(pdfPrintQueue); } catch { pdfPrintQueue?.Dispose(); throw; } }
/// <summary> /// 关闭按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click_1(object sender, RoutedEventArgs e) { PrintServer ps = new PrintServer(); PrintQueue queue = ps.GetPrintQueue(ConfigurationManager.AppSettings["printer"]); if (queue.NumberOfJobs == 0) { ps.Dispose(); queue.Dispose(); closeThis(); } else { ps.Dispose(); queue.Dispose(); MessageBox.Show("请耐心等待", "打印还未完成"); } }
public override void Start() { PrintServer PrintS = new PrintServer(); PrintQueue queue = new PrintQueue(PrintS, _window.PrintInformation.PrinterSettings.PrinterName); bool trouve; List <Document> tempDocToRemove; List <Document> tempDocInQueue; do { tempDocToRemove = new List <Document>(); tempDocInQueue = _window.DocumentsInQueue.ToList(); queue.Refresh(); foreach (Document theDoc in tempDocInQueue) { trouve = false; try { using (PrintJobInfoCollection jobinfo = queue.GetPrintJobInfoCollection()) { foreach (PrintSystemJobInfo job in jobinfo) { using (job) { if (job.Name.Contains(theDoc.Name)) { trouve = true; } } } } } catch (NullReferenceException) { } catch (RuntimeWrappedException) { } catch (InvalidOperationException) { } if (trouve == false) { tempDocToRemove.Add(theDoc); SetStatus(theDoc, State.Printed); } } foreach (Document theDoc in tempDocToRemove) { _window.DocumentsInQueue.Remove(theDoc); } } while (!_token.IsCancellationRequested || _stopWindow.Stopped); PrintS.Dispose(); queue.Dispose(); }
/// <summary> /// 得到打印机里的打印job /// </summary> /// <param name="printerName"></param> public void GetJobs(string printerName) { //if (App.psta == null) //{ // App.psta = new Models.PageSta(); //} PrintServer ps = new PrintServer(); PrintQueue queue = ps.GetPrintQueue(printerName); App.psta.nowCount = queue.NumberOfJobs; ps.Dispose(); queue.Dispose(); }
public void Dispose() { if (_printQueue != null) { _printQueue.Dispose(); _printQueue = null; } if (_printServer != null) { _printServer.Dispose(); _printServer = null; } }
public static void Print(FixedPage page)//, string pageName { //page.UpdateLayout(); PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue(); //FixedPage若直接打印,则打印出来的内容为空白,因为height为0 //推测原因:没有render //但先保存为文件,或加载到FixedDocument中就能打印,奇怪 #region System.Printing.PrintCapabilities capabilities = defaultPrintQueue.GetPrintCapabilities(); FixedDocument document = new FixedDocument(); document.DocumentPaginator.PageSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); PageContent content = new PageContent(); ((IAddChild)content).AddChild(page); document.Pages.Add(content); #endregion #region 根据打印机打印区域缩放page大小 //System.Printing.PrintCapabilities capabilities = defaultPrintQueue.GetPrintCapabilities(); ////get scale of the print wrt to screen of WPF visual //double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / page.ActualWidth, capabilities.PageImageableArea.ExtentHeight / page.ActualHeight); ////Transform the Visual to scale //page.LayoutTransform = new ScaleTransform(scale, scale); ////get the size of the printer page //Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); ////update the layout of the visual to the printer page size. //page.Measure(sz); //page.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); #endregion try { //string path = SaveXPS(page, pageName); //PrintSystemJobInfo xpsPringtJob = defaultPrintQueue.AddJob(pageName + ".xps", path, true); XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(defaultPrintQueue);//如此会自动帮我们判定上面AddJob方法的第三个参数 xpsdw.Write(document); } catch (Exception e) { MessageBox.Show("打印失败,失败原因:" + e.Message); } finally { defaultPrintQueue.Dispose(); } }
/// <summary> /// 清理打印机队列中的JOb /// </summary> public void ClearJobs() { //清空打印机里残留的队列 PrintServer ps = new PrintServer(); PrintQueue queue = ps.GetPrintQueue(ConfigurationManager.AppSettings["printer"]); queue.Refresh(); PrintJobInfoCollection allPrintJobs = queue.GetPrintJobInfoCollection(); foreach (PrintSystemJobInfo printJob in allPrintJobs) { printJob.Cancel(); } //释放资源 ps.Dispose(); queue.Dispose(); allPrintJobs.Dispose(); }
public MokaPrinter GetPrinterCapabilities(string PrinterName) { MokaPrinter thisPrinter = new MokaPrinter(); PrintServer thisPrintServer = new PrintServer(); PrintQueue thisPrintQueue = new PrintQueue(thisPrintServer, PrinterName); PrintCapabilities theCapabilities = thisPrintQueue.GetPrintCapabilities(); StringBuilder Caps = new StringBuilder(); thisPrinter.Name = PrinterName; Caps.Clear(); foreach (PageOrientation cap in theCapabilities.PageOrientationCapability) { Caps.AppendFormat("{0}={1};", (int)cap, Enum.GetName(typeof(PageOrientation), cap)); } thisPrinter.OrientationCapabilities = Caps.ToString(); Caps.Clear(); foreach (Collation cap in theCapabilities.CollationCapability) { Caps.AppendFormat("{0}={1};", (int)cap, Enum.GetName(typeof(Collation), cap)); } thisPrinter.CollationCapabilities = Caps.ToString(); Caps.Clear(); foreach (Duplexing cap in theCapabilities.DuplexingCapability) { Caps.AppendFormat("{0}={1};", (int)cap, Enum.GetName(typeof(Duplexing), cap)); } thisPrinter.DuplexingCapabilities = Caps.ToString(); Caps.Clear(); foreach (InputBin cap in theCapabilities.InputBinCapability) { Caps.AppendFormat("{0}={1};", (int)cap, Enum.GetName(typeof(InputBin), cap)); } thisPrinter.InputBinCapabilities = Caps.ToString(); Caps.Clear(); foreach (OutputColor cap in theCapabilities.OutputColorCapability) { Caps.AppendFormat("{0}={1};", (int)cap, Enum.GetName(typeof(OutputColor), cap)); } thisPrinter.OutputColorCapabilities = Caps.ToString(); Caps.Clear(); foreach (OutputQuality cap in theCapabilities.OutputQualityCapability) { Caps.AppendFormat("{0}={1};", (int)cap, Enum.GetName(typeof(OutputQuality), cap)); } thisPrinter.OutputQualityCapabilities = Caps.ToString(); Caps.Clear(); foreach (Stapling cap in theCapabilities.StaplingCapability) { Caps.AppendFormat("{0}={1};", (int)cap, Enum.GetName(typeof(Stapling), cap)); } thisPrinter.StaplingCapabilities = Caps.ToString(); thisPrintQueue.Dispose(); return(thisPrinter); }
/// <summary> /// 定时刷新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void dtimer_Tick(object sender, EventArgs e) { timenum++; PrintBLL printbll = new PrintBLL(); //PrintServer ps = new PrintServer(); //PrintQueue queue = ps.GetPrintQueue(ConfigurationManager.AppSettings["printer"]); //LocalPrintServer lps = new LocalPrintServer(); PrintQueue queue = LocalPrintServer.GetDefaultPrintQueue(); printbll.GetJobs(ConfigurationManager.AppSettings["printer"]); string str = getStatus(); if (queue.IsInError || queue.IsPaperJammed || queue.IsServerUnknown || queue.IsOutOfPaper || queue.HasPaperProblem || sameJobNum > 30)//|| str == "2" || str == "8") { wrongnum++; if (wrongnum > 3) { PrintWrong(); closeThis(); } //ps.Dispose(); queue.Dispose(); return; } //界面反馈 if (queue.NumberOfJobs > 0) { if (queue.NumberOfJobs == preJobNum) { sameJobNum++; progress.Value += 1; } else { preJobNum = queue.NumberOfJobs; sameJobNum = 0; progress.Value = (100 * (App.psta.Count - preJobNum) / App.psta.Count); } num = 0; nowCountText.Text = (App.psta.Count - preJobNum).ToString(); //ps.Dispose(); queue.Dispose(); return; } //打印结束 if (queue.NumberOfJobs == 0) { progress.Value = 100; num++; //ps.Dispose(); queue.Dispose(); if (num >= 3) { //成功结束 if (PrintSuccess() == 0 || num > 6) { if (num > 6) { messgeBoxBll.Show("与服务器通信失败", "因网络问题通信失败,本次打印不会扣费"); } closeThis(); } } } }
public void Dispose() { _printDocument?.Dispose(); _printServer?.Dispose(); _printQueue?.Dispose(); }
/// <summary> /// 状态检测方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void stadtimer_Tick(object sender, EventArgs e) { System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions(); options.DontFragment = true; string data = "Test Data!"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 2000; // Timeout 时间,单位:毫秒 //检查网络连接 try { System.Net.NetworkInformation.PingReply reply = p.Send("baidu.com", timeout, buffer, options); if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { stat.line = true; netLine.Text = "网络已连接"; netLine.Foreground = Brushes.Green; } else { stat.line = false; netLine.Text = "网络连接已断开"; netLine.Foreground = Brushes.Red; } } catch { stat.line = false; netLine.Text = "网络异常,请联系管理员"; netLine.Foreground = Brushes.Red; } if (stat.line == true) { //从网络获取机器状态 HttpBLL httpbll = new HttpBLL(); JSONBLL jsonbll = new JSONBLL(); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("id", ConfigurationManager.AppSettings["id"]); string str = httpbll.GetResponseString(httpbll.CreatePostHttpResponse(ConfigurationManager.AppSettings["machine"], dic)); if (str != null) { JObject jo = JsonConvert.DeserializeObject <JObject>(str); if (jo["code"].ToString() == "200") { JObject jo1; string str1 = jo["machine"].ToString(); if (str1 != null) { jsonbll.jsonToJobject(str1, out jo1); stat.stat = jo1["nowStatus"].ToString(); } else { stat.stat = "查无此机,若要正常使用,请检查配置文件与服务器"; } } else { stat.stat = "意外错误,未能与服务器正常通信"; } } else { stat.stat = "意外错误,通信失败"; } } else { stat.stat = "连接失败"; } pageRemain.Text = App.set.remainPageNum.ToString(); try { PrintServer ps = new PrintServer(); PrintQueue queue = ps.GetPrintQueue(ConfigurationManager.AppSettings["printer"]); printerStaTxt.Text = queue.QueueStatus.ToString(); ps.Dispose(); queue.Dispose(); } catch { printerStaTxt.Text = "打印服务未启动"; } }
public void TearDown() { printQueue.Dispose(); spoolWatcher.Dispose(); }
public override void Start() { _wait.Reset(); _inQueue = false; PrintServer PrintS = new PrintServer(); PrintQueue queue = new PrintQueue(PrintS, _window.PrintInformation.PrinterSettings.PrinterName); int trial = 0; do { trial++; queue.Refresh(); if (trial >= 3000 && trial < 6000) { RefreshList(_document); _window.Dispatcher.Invoke(new Action(() => { _stopWindow.skipVisibility(System.Windows.Visibility.Visible); })); SetStatus(_document, State.Searching); } else if (trial >= 6000) { SetStatus(_document, State.StillSearching); } try { if (queue.NumberOfJobs > 0) { using (PrintSystemJobInfo job = queue.GetPrintJobInfoCollection().Last()) { if (job.Name.Contains(_document.Name)) { _inQueue = true; SetStatus(_document, State.InQueue); log.Info(_document.Name + " has been queued"); } } } } catch (NullReferenceException) { } catch (RuntimeWrappedException) { } catch (InvalidOperationException) { } } while (!_inQueue && trial < 10000 && !_token.IsCancellationRequested && !_stopWindow.Skip); PrintS.Dispose(); queue.Dispose(); if (trial >= 10000) { SetStatus(_document, State.Error); log.Error(_document.Name + " has made an error"); if (!_window.Configuration.AutoRetry) { System.Media.SystemSounds.Beep.Play(); _window.Dispatcher.Invoke(new Action(() => { _stopWindow.retryVisibility(System.Windows.Visibility.Visible); _window.Activate(); _stopWindow.Activate(); })); _stopWindow.Retry.Reset(); _stopWindow.Retry.WaitOne(); } } if (_stopWindow.Skip) { SetStatus(_document, State.Skipped); log.Info(_document.Name + " has been skipped"); } }