void PrintDocument_PrintPage(object sender, PrintPageEventArgs e) { if (this.Font == null || this.Document == null) { throw new InvalidOperationException(); } if (view == null) { this.render = new PrintableTextRender(this.Font, e.Graphics); this.render.Foreground = this.Foreground; this.render.RightToLeft = this.RightToLeft; Document documentSnap = new Document(this.Document); documentSnap.LayoutLines.Render = render; this.view = new PrintableView(documentSnap, this.render, this.Padding); this.view.PageBound = e.MarginBounds; this.PageNumber = 1; documentSnap.LineBreak = this.LineBreakMethod; documentSnap.LineBreakCharCount = this.LineBreakCharCount; documentSnap.DrawLineNumber = this.DrawLineNumber; documentSnap.UrlMark = this.Document.UrlMark; documentSnap.PerformLayout(false); } if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { for (; this.PageNumber < e.PageSettings.PrinterSettings.FromPage; this.PageNumber++) { if (this.view.TryPageDown()) { return; } } } this.view.Header = this.ParseHF(this, new ParseCommandEventArgs(this.PageNumber, e.PageSettings.PrinterSettings, this.Header)); this.view.Footer = this.ParseHF(this, new ParseCommandEventArgs(this.PageNumber, e.PageSettings.PrinterSettings, this.Footer)); this.render.BeginDraw(e.Graphics); this.view.Draw(e.MarginBounds); e.HasMorePages = !this.view.TryPageDown(); this.render.EndDraw(); this.PageNumber++; if (e.HasMorePages && e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages && this.PageNumber > e.PageSettings.PrinterSettings.ToPage) { e.HasMorePages = false; } }
void PrintDocument_EndPrint(object sender, PrintEventArgs e) { this.view.Dispose(); this.view = null; this.render = null; }