public void Run(Pages pages, PageOrientation pageOrientation) { _pages = pages; PrintOperation printOperation = null; try { printOperation = new PrintOperation(); printOperation.Unit = Unit.Points; printOperation.UseFullPage = true; printOperation.DefaultPageSetup = new PageSetup(); printOperation.DefaultPageSetup.Orientation = pageOrientation; printOperation.BeginPrint += HandlePrintBeginPrint; printOperation.DrawPage += HandlePrintDrawPage; printOperation.Run(PrintOperationAction.PrintDialog, null); } catch (Exception e) when(e.Message == "Error from StartDoc") { _logger.Debug("Операция печати отменена"); } finally { if (printOperation != null) { printOperation.BeginPrint -= HandlePrintBeginPrint; printOperation.DrawPage -= HandlePrintDrawPage; printOperation.Dispose(); } } }
private static void Print(string printer, UserPrintSettings userPrintSettings, Pages pages, bool isWindowsOs) { void HandlePrintBeginPrint(object o, BeginPrintArgs args) { var printing = (PrintOperation)o; printing.NPages = pages.Count; } void HandlePrintDrawPage(object o, DrawPageArgs args) { using (Cairo.Context g = args.Context.CairoContext) { var render = new RenderCairo(g); render.RunPage(pages[args.PageNr]); } } PrintOperation printOperation = null; PrintOperationResult result; try { printOperation = new PrintOperation(); printOperation.Unit = Unit.Points; printOperation.UseFullPage = true; printOperation.AllowAsync = true; printOperation.PrintSettings = new PrintSettings { Printer = printer, Orientation = (PageOrientation)Enum.Parse(typeof(PageOrientation), userPrintSettings.PageOrientation.ToString()), NCopies = (int)userPrintSettings.NumberOfCopies }; printOperation.BeginPrint += HandlePrintBeginPrint; printOperation.DrawPage += HandlePrintDrawPage; result = printOperation.Run(PrintOperationAction.Print, null); } catch (Exception e) when(e.Message == "Error from StartDoc") { result = PrintOperationResult.Cancel; _logger.Debug("Операция печати отменена"); } finally { if (printOperation != null) { printOperation.BeginPrint -= HandlePrintBeginPrint; printOperation.DrawPage -= HandlePrintDrawPage; printOperation.Dispose(); } } if (isWindowsOs && new[] { PrintOperationResult.Apply, PrintOperationResult.InProgress }.Contains(result)) { ShowPrinterQueue(printer); } }
void HandlePrintEndPrint(object o, EndPrintArgs args) { ReportPrinted?.Invoke(this, EventArgs.Empty); printing.BeginPrint -= HandlePrintBeginPrint; printing.DrawPage -= HandlePrintDrawPage; printing.EndPrint -= HandlePrintEndPrint; printing.Dispose(); }
public void Dispose() { printOperation.Dispose(); }