Windows Develop Bookmark and Share   
 index > Windows Forms General > Finding MarginBound in PrintDocument
 

Finding MarginBound in PrintDocument

When inheriting from System.Drawing.Printing.PrintDocument is there a way to get the page margin bound before the OnPrintPage event?

 

I'm only able to get it from the OnPrintPage event but this is being called for every pages and I would like to initialize my image before the first page is being called.

 

The ideal place would be in the OnBeginPrint event.

ThE_lOtUs  Thursday, January 12, 2006 7:30 PM
Technically you can't, the QueryPageSettings event allows the client to change the page settings for each individual page. If you have to disallow this then just lazily initialize your images on the first invocation of OnPrintPage:

using System;
using System.Drawing;
using System.Drawing.Printing;

class MyDocument : PrintDocument {
bool mInitialized;
Rectangle mPageBounds;

private void InitializeImages(Rectangle bounds) {
// etc..
}
protected override void OnBeginPrint(PrintEventArgs e) {
mInitialized = false;
base.OnBeginPrint(e);
}
protected override void OnPrintPage(PrintPageEventArgs e) {
if (!mInitialized) {
mInitialized = true;
mPageBounds = e.PageBounds;
InitializeImages(e.PageBounds);
}
else {
if (e.PageBounds != mPageBounds) throw new InvalidOperationException("Page bounds have changed");
}
base.OnPrintPage(e);
}
}

Hans Passant.
nobugz  Sunday, September 27, 2009 6:50 PM

Private Function GatherDefaultMarginBounds() As Rectangle
Dim oMargins As Margins = Me.DefaultPageSettings.Margins
Dim oPageBounds As Rectangle = Me.DefaultPageSettings.Bounds
Return Rectangle.FromLTRB(oMargins.Left, oMargins.Top, oPageBounds.Right - oMargins.Right, oPageBounds.Bottom - oMargins.Bottom)
End Function

AMissico  Sunday, September 27, 2009 4:47 PM
Technically you can't, the QueryPageSettings event allows the client to change the page settings for each individual page. If you have to disallow this then just lazily initialize your images on the first invocation of OnPrintPage:

using System;
using System.Drawing;
using System.Drawing.Printing;

class MyDocument : PrintDocument {
bool mInitialized;
Rectangle mPageBounds;

private void InitializeImages(Rectangle bounds) {
// etc..
}
protected override void OnBeginPrint(PrintEventArgs e) {
mInitialized = false;
base.OnBeginPrint(e);
}
protected override void OnPrintPage(PrintPageEventArgs e) {
if (!mInitialized) {
mInitialized = true;
mPageBounds = e.PageBounds;
InitializeImages(e.PageBounds);
}
else {
if (e.PageBounds != mPageBounds) throw new InvalidOperationException("Page bounds have changed");
}
base.OnPrintPage(e);
}
}

Hans Passant.
nobugz  Sunday, September 27, 2009 6:50 PM

You can use google to search for other answers

Custom Search

More Threads

• doubleclick after mousedown
• TabControl.TabPages.Clear() hangs the application.
• Form Templates
• Instance of MessageBox Class in c#!
• list all available Servers
• resize of user control within user control within form
• WHERE DO I REPORT ISSUE IN MS COMBOBOX ??
• Passwords!
• Window.Forms: menu appears after headline click [Solved]
• httpwebrequest