public PrintPreviewController(GtkFormPrintDocument operation)
 {
     this.operation        = operation;
     operation.BeginPrint += operation_BeginPrint;
     operation.DrawPage   += operation_DrawPage;
     operation.EndPrint   += operation_EndPrint;
 }
        public void PrintDocument()
        {
            if (printingDocument)
            {
                return;
            }

            try {
                printingDocument  = true;
                exceptionPrinting = null;
                if (document != null)
                {
                    var newDocument = new GtkFormPrintDocument(document.FormToPrint)
                    {
                        UseFullPage = document.UseFullPage
                    };
                    newDocument.DefaultPageSetup             = document.DefaultPageSetup.Copy();
                    newDocument.DefaultPageSetup.Orientation = document.FormToPrint.Landscape
                        ? PageOrientation.Landscape
                        : PageOrientation.Portrait;
                    if (document.PrintSettings != null)
                    {
                        newDocument.PrintSettings             = document.PrintSettings.Copy();
                        newDocument.PrintSettings.Orientation = newDocument.DefaultPageSetup.Orientation;
                    }
                    string printerName = null;
                    if (document.PrintSettings != null && !string.IsNullOrWhiteSpace(document.PrintSettings.Printer))
                    {
                        printerName = document.PrintSettings.Printer;
                    }

                    newDocument.SetPrinterSettings(printerName);

                    ClearPagesCache();

                    document             = newDocument;
                    sizeIsInvalid        = true;
                    virtualSizeIsInvalid = true;

                    document.DrawPage += document_DrawPage;
                    using (new PrintPreviewController(document))
                        document.Run(PrintOperationAction.Preview, ComponentHelper.TopWindow);
                    document.DrawPage -= document_DrawPage;

                    document.PageRows    = document.FormToPrint.GetPageRows();
                    document.PageColumns = document.FormToPrint.GetPageColumns();
                    StartPage            = Math.Min(startPage, TotalPages - 1);
                    OnPagesCalculated(EventArgs.Empty);
                }
                QueueDraw();
            } catch (GException ex) {
                exceptionPrinting = ex;
            } catch (AccessViolationException ex) {
                exceptionPrinting = ex;
            } finally {
                printingDocument = false;
            }
        }