private bool PrintLoopOptimized(PrintDocument document) { PrintPageEventArgs pageEvent = null; PageSettings documentPageSettings = (PageSettings)document.DefaultPageSettings.Clone(); QueryPageSettingsEventArgs queryEvent = new QueryPageSettingsEventArgs(documentPageSettings); for (;;) { queryEvent.PageSettingsChanged = false; document._OnQueryPageSettings(queryEvent); if (queryEvent.Cancel) { return(true); } if (!queryEvent.PageSettingsChanged) { // QueryPageSettings event handler did not change the page settings, // thus we use default page settings from the document object. if (pageEvent == null) { pageEvent = CreatePrintPageEvent(queryEvent.PageSettings); } else { // This is not the first page and the settings had not changed since the previous page, // thus don't re-apply them. pageEvent.CopySettingsToDevMode = false; } Graphics graphics = OnStartPage(document, pageEvent); pageEvent.SetGraphics(graphics); } else { // Page settings were customized, so use the customized ones in the start page event. pageEvent = CreatePrintPageEvent(queryEvent.PageSettings); Graphics graphics = OnStartPage(document, pageEvent); pageEvent.SetGraphics(graphics); } try { document._OnPrintPage(pageEvent); OnEndPage(document, pageEvent); } finally { pageEvent.Graphics.Dispose(); pageEvent.SetGraphics(null); } if (pageEvent.Cancel) { return(true); } else if (!pageEvent.HasMorePages) { return(false); } } }
public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) { var result = PMSessionBeginPageNoDialog( sessionHandle, e.PageSettings == null || e.PageSettings == document.DefaultPageSettings ? IntPtr.Zero : e.PageSettings.print_info.GetPMPageFormat(), IntPtr.Zero); IntPtr contextHandle; PMSessionGetCGGraphicsContext(sessionHandle, out contextHandle); e.SetGraphics(new Graphics(new CGContext(contextHandle), false)); return(e.Graphics); }
// Returns true if print was aborted. // WARNING: if you have nested PrintControllers, this method won't get called on the inner one // Add initialization code to StartPrint or StartPage instead. private bool PrintLoop(PrintDocument document) { QueryPageSettingsEventArgs queryEvent = new QueryPageSettingsEventArgs((PageSettings)document.DefaultPageSettings.Clone()); for (;;) { document._OnQueryPageSettings(queryEvent); if (queryEvent.Cancel) { return(true); } PrintPageEventArgs pageEvent = CreatePrintPageEvent(queryEvent.PageSettings); Graphics graphics = OnStartPage(document, pageEvent); pageEvent.SetGraphics(graphics); try { document._OnPrintPage(pageEvent); OnEndPage(document, pageEvent); } finally { pageEvent.Dispose(); } if (pageEvent.Cancel) { return(true); } else if (!pageEvent.HasMorePages) { return(false); } else { // loop } } }
// methods public void Print() { PrintEventArgs printArgs = new PrintEventArgs(); this.OnBeginPrint(printArgs); if (printArgs.Cancel) { return; } PrintController.OnStartPrint(this, printArgs); if (printArgs.Cancel) { return; } Graphics g = null; if (printArgs.GraphicsContext != null) { g = Graphics.FromHdc(printArgs.GraphicsContext.Hdc); printArgs.GraphicsContext.Graphics = g; } // while there are more pages PrintPageEventArgs printPageArgs; do { QueryPageSettingsEventArgs queryPageSettingsArgs = new QueryPageSettingsEventArgs( DefaultPageSettings.Clone() as PageSettings); OnQueryPageSettings(queryPageSettingsArgs); PageSettings pageSettings = queryPageSettingsArgs.PageSettings; printPageArgs = new PrintPageEventArgs( g, pageSettings.Bounds, new Rectangle(0, 0, pageSettings.PaperSize.Width, pageSettings.PaperSize.Height), pageSettings); // TODO: We should create a graphics context for each page since they can have diferent paper // size, orientation, etc. We use a single graphic for now to keep Cairo using a single PDF file. printPageArgs.GraphicsContext = printArgs.GraphicsContext; Graphics pg = PrintController.OnStartPage(this, printPageArgs); // assign Graphics in printPageArgs printPageArgs.SetGraphics(pg); if (!printPageArgs.Cancel) { this.OnPrintPage(printPageArgs); } PrintController.OnEndPage(this, printPageArgs); if (printPageArgs.Cancel) { break; } } while (printPageArgs.HasMorePages); this.OnEndPrint(printArgs); PrintController.OnEndPrint(this, printArgs); }
// methods public void Print(){ PrintEventArgs printArgs = new PrintEventArgs(); this.OnBeginPrint(printArgs); if (printArgs.Cancel) return; PrintController.OnStartPrint(this, printArgs); if (printArgs.Cancel) return; Graphics g = null; if (printArgs.GraphicsContext != null) { g = Graphics.FromHdc (printArgs.GraphicsContext.Hdc); printArgs.GraphicsContext.Graphics = g; } // while there are more pages PrintPageEventArgs printPageArgs; do { QueryPageSettingsEventArgs queryPageSettingsArgs = new QueryPageSettingsEventArgs ( DefaultPageSettings.Clone () as PageSettings); OnQueryPageSettings (queryPageSettingsArgs); PageSettings pageSettings = queryPageSettingsArgs.PageSettings; printPageArgs = new PrintPageEventArgs( g, pageSettings.Bounds, new Rectangle(0, 0, pageSettings.PaperSize.Width, pageSettings.PaperSize.Height), pageSettings); // TODO: We should create a graphics context for each page since they can have diferent paper // size, orientation, etc. We use a single graphic for now to keep Cairo using a single PDF file. printPageArgs.GraphicsContext = printArgs.GraphicsContext; Graphics pg = PrintController.OnStartPage(this, printPageArgs); // assign Graphics in printPageArgs printPageArgs.SetGraphics(pg); if (!printPageArgs.Cancel) this.OnPrintPage(printPageArgs); PrintController.OnEndPage(this, printPageArgs); if (printPageArgs.Cancel) break; } while (printPageArgs.HasMorePages); this.OnEndPrint(printArgs); PrintController.OnEndPrint(this, printArgs); }