Example #1
0
        /// <remarks>
        /// If you have nested PrintControllers, this method won't get called on the inner one.
        /// Add initialization code to StartPrint or StartPage instead.
        /// </remarks>
        internal void Print(PrintDocument document)
        {
            // Get the PrintAction for this event
            PrintAction printAction;

            if (IsPreview)
            {
                printAction = PrintAction.PrintToPreview;
            }
            else
            {
                printAction = document.PrinterSettings.PrintToFile ? PrintAction.PrintToFile : PrintAction.PrintToPrinter;
            }

            // Check that user has permission to print to this particular printer
            PrintEventArgs printEvent = new PrintEventArgs(printAction);

            document.OnBeginPrint(printEvent);
            if (printEvent.Cancel)
            {
                document.OnEndPrint(printEvent);
                return;
            }

            OnStartPrint(document, printEvent);
            if (printEvent.Cancel)
            {
                document.OnEndPrint(printEvent);
                OnEndPrint(document, printEvent);
                return;
            }

            bool canceled = true;

            try
            {
                // To enable optimization of the preview dialog, add the following to the config file:
                // <runtime >
                //     <!-- AppContextSwitchOverrides values are in the form of 'key1=true|false;key2=true|false  -->
                //     <AppContextSwitchOverrides value = "Switch.System.Drawing.Printing.OptimizePrintPreview=true" />
                // </runtime >
                canceled = LocalAppContextSwitches.OptimizePrintPreview ? PrintLoopOptimized(document) : PrintLoop(document);
            }
            finally
            {
                try
                {
                    document.OnEndPrint(printEvent);
                    printEvent.Cancel = canceled | printEvent.Cancel;
                }
                finally
                {
                    OnEndPrint(document, printEvent);
                }
            }
        }