public DialogResult ShowDialog (Control parent, PrintDocument document) { this.PrintSettings.MaximumPageRange = new Range (1, document.PageCount); this.PrintSettings = document.PrintSettings; var result = this.ShowDialog (parent); if (result == DialogResult.Ok) { document.Print (); } return result; }
/// <summary> /// Shows the print dialog for the specified <paramref name="document"/>, printing after closed if the user selects to print. /// </summary> /// <returns>The result.</returns> /// <param name="parent">Parent of the dialog to make modal.</param> /// <param name="document">Document to print.</param> public DialogResult ShowDialog(Control parent, PrintDocument document) { PrintSettings = document.PrintSettings; PrintSettings.MaximumPageRange = new Range<int>(1, document.PageCount); Handler.Document = document; var result = ShowDialog(parent); if (result == DialogResult.Ok) { document.Print(); } return result; }
PrintDocument GetPrintDocument() { var document = new PrintDocument(); document.PrintSettings = settings; var font = Fonts.Serif(16); var printTime = DateTime.Now; document.PrintPage += (sender, e) => { Size pageSize = Size.Round(e.PageSize); // draw a border around the printable area var rect = new Rectangle(pageSize); rect.Inflate(-1, -1); e.Graphics.DrawRectangle(Pens.Silver, rect); // draw title e.Graphics.DrawText(font, Colors.Black, new Point(50, 20), document.Name); // draw page number var text = string.Format("page {0} of {1}", e.CurrentPage + 1, document.PageCount); var textSize = Size.Round(e.Graphics.MeasureString(font, text)); e.Graphics.DrawText(font, Colors.Black, new Point(pageSize.Width - textSize.Width - 50, 20), text); // draw date text = string.Format("Printed on {0:f}", printTime); textSize = Size.Round(e.Graphics.MeasureString(font, text)); e.Graphics.DrawText(font, Colors.Black, new Point(pageSize.Width - textSize.Width - 50, pageSize.Height - textSize.Height - 20), text); // draw some rectangles switch (e.CurrentPage) { case 0: e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(50, 50, 100, 100)); e.Graphics.DrawRectangle(Pens.Green, new Rectangle(new Point(pageSize) - new Size(150, 150), new Size(100, 100))); break; case 1: e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(pageSize.Width - 150, 50, 100, 100)); e.Graphics.DrawRectangle(Pens.Green, new Rectangle(50, pageSize.Height - 150, 100, 100)); break; } }; document.Name = "Name Of Document"; document.PageCount = 2; return document; }
/// <summary> /// Raises the print page event. /// </summary> public void OnPrintPage(PrintDocument widget, PrintPageEventArgs e) { widget.Platform.Invoke(() => widget.OnPrintPage(e)); }
/// <summary> /// Raises the printed event. /// </summary> public void OnPrinted(PrintDocument widget, EventArgs e) { widget.Platform.Invoke(() => widget.OnPrinted(e)); }
/// <summary> /// Initializes a new instance of the PrintPreviewDialog for the specified <paramref name="document"/>. /// </summary> /// <param name="document">Print document to preview</param> public PrintPreviewDialog(PrintDocument document) { Document = document ?? throw new ArgumentNullException(nameof(document)); }