private void ConvertToStream(string fileUrl) { try { this.events.PublishOnUIThread(new ViewStartPrintProgressCommand()); } catch (Exception ex) { this.log.Info("InvokingAction: ViewException " + ex.ToString()); } System.Threading.Thread.Sleep(3000); try { ServicePointManager .ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); var theStream = new object(); string fileName = AppDomain.CurrentDomain.BaseDirectory + "Printings" + @"\toPrint.pdf"; using (var stream = File.Create(fileName)) { File.SetAttributes(fileName, FileAttributes.Normal); response.GetResponseStream().CopyTo(stream); } iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(fileName); int numberOfPages = pdfReader.NumberOfPages; if (GlobalCounters.numberOfCurrentPrintings + numberOfPages <= Convert.ToInt32(this.numberOfAvailabelPagesToPrint)) { try { this.log.Info("Invoking Action: ViewPrintRequested " + numberOfPages.ToString() + " pages."); } catch { } try { TaskbarManager.HideTaskbar(); } catch { } try { System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); info.UseShellExecute = true; info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; info.Verb = "print"; info.FileName = fileName; info.CreateNoWindow = true; System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = info; p.Start(); p.WaitForExit(); p.CloseMainWindow(); if (false == p.CloseMainWindow()) { try { p.Kill(); } catch { } } else { try { p.Kill(); } catch { } } } catch { } GlobalCounters.numberOfCurrentPrintings += numberOfPages; try { TaskbarManager.HideTaskbar(); } catch { } System.Threading.Thread.Sleep(TimeSpan.FromSeconds(6)); try { TaskbarManager.HideTaskbar(); } catch { } try { this.sender.SendAction("Printed " + numberOfPages + " pages."); } catch { } } else { System.Windows.MessageBox.Show("Unfortunately, you can not print so many pages! Please press OK to continue."); } } catch (Exception ex) { this.log.Info("InvokingAction: ViewException " + ex.ToString()); } finally { } }
public async void onPrintRequested(string selected) { try { this.events.PublishOnUIThread(new ViewStartPrintProgressCommand()); } catch { } // System.Threading.Thread.Sleep(2000); if (selected == "before") { // PrinterCanceller.CancelPrint(); string path = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( this.GetType().Assembly.Location), "Printings", GlobalCounters.numberOfCurrentPrintings.ToString() + ".pdf"); var success = await _mainBrowser.PrintToPdfAsync(path, new PdfPrintSettings { MarginType = CefPdfPrintMarginType.Custom, MarginBottom = 10, MarginTop = 0, MarginLeft = 20, MarginRight = 10, }); System.Threading.Thread.Sleep(3000); if (success) { try { iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(path); int numberOfPages = pdfReader.NumberOfPages; if (GlobalCounters.numberOfCurrentPrintings + numberOfPages <= Convert.ToInt32(this.numberOfAvailablePagesToPrint)) { try { this.log.Info("Invoking Action: ViewPrintRequested " + numberOfPages.ToString() + " pages."); } catch { } try { TaskbarManager.HideTaskbar(); } catch { } try { System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); info.UseShellExecute = true; info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; info.Verb = "print"; info.FileName = path; info.CreateNoWindow = true; System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = info; p.Start(); // p.WaitForInputIdle(); p.WaitForExit(); p.CloseMainWindow(); if (false == p.CloseMainWindow()) { try { p.Kill(); } catch { } } else { try { p.Kill(); } catch { } } } catch { } GlobalCounters.numberOfCurrentPrintings += numberOfPages; try { TaskbarManager.HideTaskbar(); } catch { } System.Threading.Thread.Sleep(TimeSpan.FromSeconds(6)); try { TaskbarManager.HideTaskbar(); } catch { } try { this.sender.SendAction("Printed " + numberOfPages + " pages."); } catch { } } else { System.Windows.MessageBox.Show("Unfortunately, you can not print so many pages! Please press OK to continue."); } } catch (Exception ex) { this.log.Info("Exception: " + ex.ToString()); } } //else //{ // System.Windows.MessageBox.Show("Failed to print cause unexptected problem! Please try again.."); //} } /* try * { * KillAdobe("AcroRd32"); * } * catch { }*/ }
public async void onPrintRequested(string selected) { try { this.events.PublishOnUIThread(new ViewStartPrintProgressCommand()); } catch { } System.Threading.Thread.Sleep(2000); if (selected == "before") { // PrinterCanceller.CancelPrint(); string path = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( this.GetType().Assembly.Location), "Printings", GlobalCounters.numberOfCurrentPrintings.ToString() + ".pdf"); var success = await _mainBrowser.PrintToPdfAsync(path, new PdfPrintSettings { MarginType = CefPdfPrintMarginType.Custom, MarginBottom = 10, MarginTop = 0, MarginLeft = 20, MarginRight = 10, }); System.Threading.Thread.Sleep(3000); if (success) { try { iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(path); int numberOfPages = pdfReader.NumberOfPages; if (GlobalCounters.numberOfCurrentPrintings + numberOfPages <= Convert.ToInt32(this.numberOfAvailablePagesToPrint)) { try { this.log.Info("Invoking Action: ViewPrintRequested " + numberOfPages.ToString() + " pages."); } catch { } try { System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; info.Verb = "print"; info.FileName = path; info.CreateNoWindow = true; System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = info; p.Start(); p.WaitForExit(); p.CloseMainWindow(); if (false == p.CloseMainWindow()) { try { p.Kill(); } catch { } } else { try { p.Kill(); } catch { } } try { TaskbarManager.HideTaskbar(); } catch { } } catch { } GlobalCounters.numberOfCurrentPrintings += numberOfPages; try { TaskbarManager.HideTaskbar(); } catch { } System.Threading.Thread.Sleep(6000); try { TaskbarManager.HideTaskbar(); } catch { } try { this.sender.SendAction("Printed " + numberOfPages + " pages."); } catch { } } else { System.Windows.MessageBox.Show("Δυστυχώς, δεν μπορείτε να εκτυπώσετε τόσσες σελίδες. Πατήστε OK για να συνεχίσετε."); } } catch (Exception ex) { this.log.Info("Exception: " + ex.ToString()); } } // else //{ // System.Windows.MessageBox.Show("Αποτυχία εκτύπωσης! Παρακαλώ, δοκιμάστε ξανά.."); //} } /* try * { * KillAdobe("AcroRd32"); * } * catch { }*/ }