Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / C#

Getting the printer's REAL margin bounds in .NET

Rate me:
Please Sign up or sign in to vote.
4.60/5 (33 votes)
1 Jul 20043 min read 158.4K   3.5K   46   26
The .NET printing classes don't take into account the printer's physical margins. This class fixes this problem, so your printed pages will be using the correct margins.

Introduction

In this article, I present a very useful class that works around a serious limitation in the .NET printing classes. The class PrinterBounds retrieves the real printing bounds of a printed page. The .NET printing classes don't take into account the physical left and top margins of the printer.

Background

When you want to print a document using .NET, you create a PrintDocument object, and subscribe to the PrintPage event handler. Let's say you want to set the page margins to 1 inch on each side, and draw a 2 inch rectangle in the top-left corner of the page (using these margins):

C#
private void printDoc_PrintPage(object sender,
  System.Drawing.Printing.PrintPageEventArgs e)
{
  Rectangle r = e.MarginBounds;

  e.Graphics.DrawRectangle(Pens.Black , r.Left , r.Top , 200 , 200);

  e.HasMorePages = false;
}

Looks easy, right? But there's a serious problem when you look at the printed output (it looks fine in a print preview). You will notice that the rectangle is not at 1 inch from the left and top edge, but maybe 1.2 inches.

The reason is simple: The MarginBounds property doesn't know anything about the physical left and right margins for your printer (the non-printable region), and .NET provides no way to find out what these margins are. On the other hand, the width and height of the MarginBounds rectangle DOES take into account the hard margins of your printer, but the offset (Left and Top) is incorrect. (I have no idea why Microsoft didn't fix this in v1.1 of the Framework.)

The PrinterBounds class presented here solves this problem.

Implementation

To determine the printer's physical margins, we need to call a Win32 function from gdi32.dll: GetDeviceCaps()

C#
[DllImport("gdi32.dll")] private static extern Int32 
  GetDeviceCaps(IntPtr hdc, Int32 capindex);

We can then use it like this:

C#
HardMarginLeft = GetDeviceCaps(hDC , PHYSICALOFFSETX);
HardMarginTop  = GetDeviceCaps(hDC , PHYSICALOFFSETY);

Of course, we need the handle to the device context of the printer, which is not too hard, since we have a reference to the Graphics object in the PrintPageEventArgs parameter of the PrintPage event handler:

C#
IntPtr hDC = e.Graphics.GetHdc(); // Get the device context handle

HardMarginLeft = GetDeviceCaps(hDC , PHYSICALOFFSETX);
HardMarginTop  = GetDeviceCaps(hDC , PHYSICALOFFSETY);

e.Graphics.ReleaseHdc(hDC); // Don't forget to release it again

Now we have the margins in device units. To convert these to printer units (1/100 inch), we have to get the DPI of the printer, which is available in the DpiX and DpiY properties of our Graphics object.

Remember I mentioned that the MarginBounds does have the correct width and height, so we just need to shift the rectangle to the correct position, using the hard margins we just retrieved. The final class looks like this:

C#
public class PrinterBounds
{
  [DllImport("gdi32.dll")] private static extern Int32 
     GetDeviceCaps(IntPtr hdc, Int32 capindex);

  private const int PHYSICALOFFSETX = 112;
  private const int PHYSICALOFFSETY = 113;

  public readonly Rectangle Bounds;
  public readonly int       HardMarginLeft;
  public readonly int       HardMarginTop;

  public PrinterBounds(PrintPageEventArgs e)
  {
    IntPtr hDC = e.Graphics.GetHdc();

    HardMarginLeft = GetDeviceCaps(hDC , PHYSICALOFFSETX);
    HardMarginTop  = GetDeviceCaps(hDC , PHYSICALOFFSETY);

    e.Graphics.ReleaseHdc(hDC);

    HardMarginLeft = (int)(HardMarginLeft * 100.0 / e.Graphics.DpiX);
    HardMarginTop  = (int)(HardMarginTop  * 100.0 / e.Graphics.DpiY);

    Bounds = e.MarginBounds;

    Bounds.Offset(-HardMarginLeft , -HardMarginTop);
  }
}

Using the Code

Using the class is very simple. In your PrintPage event handler, use an instance of PrinterBounds instead of the MarginBounds property of PrintPageEventArgs:

C#
private void printDoc_PrintPage(object sender, 
   System.Drawing.Printing.PrintPageEventArgs e)
{
  PrinterBounds objBounds = new PrinterBounds(e);

  Rectangle r = objBounds.Bounds;  // Get the REAL Margin Bounds !
  
  e.Graphics.DrawRectangle(Pens.Black , r.Left , r.Top , 200 , 200);

  e.HasMorePages = false;
}

Points of Interest

Microsoft knew about this problem long before v1.1 of the .NET Framework was released, but they didn't fix this bug. I suppose they didn't want to break existing code. I haven't checked the v2.0 beta of the Framework, but I suspect they left it like this.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
Web Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to get Printable Area margin bound in .Net printDocument C# Pin
Member 129492729-Apr-21 15:23
Member 129492729-Apr-21 15:23 
Questioncode not do any thing Pin
Member 129492729-Apr-21 13:58
Member 129492729-Apr-21 13:58 
GeneralThanks Pin
zapper00114-Jul-10 16:23
zapper00114-Jul-10 16:23 
GeneralMy vote of 1 Pin
Member 43719156-Jan-10 5:35
Member 43719156-Jan-10 5:35 
QuestionHow to get dpi of the unprintable area of a printer? Pin
egkua8-Dec-09 17:06
egkua8-Dec-09 17:06 
GeneralThank you Pin
Nagarajanp15-Nov-06 22:38
Nagarajanp15-Nov-06 22:38 
GeneralWe need NOT to call API to obtain hard margin in .net Pin
super gun tank24-Sep-06 0:19
super gun tank24-Sep-06 0:19 
GeneralRe: We need NOT to call API to obtain hard margin in .net Pin
Philippe Leybaert24-Sep-06 9:03
Philippe Leybaert24-Sep-06 9:03 
GeneralRe: We need NOT to call API to obtain hard margin in .net Pin
termi25-Oct-06 2:11
termi25-Oct-06 2:11 
GeneralRe: We need NOT to call API to obtain hard margin in .net Pin
Philippe Leybaert25-Oct-06 4:54
Philippe Leybaert25-Oct-06 4:54 
GeneralRe: We need NOT to call API to obtain hard margin in .net Pin
ArtiBen16-May-07 3:59
ArtiBen16-May-07 3:59 
QuestionCentering text on a page Pin
salblomo24-Aug-06 10:19
salblomo24-Aug-06 10:19 
AnswerRe: Centering text on a page Pin
Philippe Leybaert24-Aug-06 10:45
Philippe Leybaert24-Aug-06 10:45 
GeneralUnable to set Left Margin Pin
N. Hoyek18-Apr-05 11:36
N. Hoyek18-Apr-05 11:36 
GeneralRe: Unable to set Left Margin Pin
N. Hoyek20-Apr-05 3:43
N. Hoyek20-Apr-05 3:43 
QuestionDeterming the PrinterBounds without printing? Pin
ashtekar16-Feb-05 5:31
ashtekar16-Feb-05 5:31 
AnswerRe: Determing the PrinterBounds without printing? Pin
5-Jun-05 0:56
suss5-Jun-05 0:56 
QuestionWhat about PrintPreviewDialog Pin
bhelzer13-Jan-05 10:56
bhelzer13-Jan-05 10:56 
AnswerHere's how to know if you are in preview or print mode Pin
[Marc]6-Jul-05 17:16
[Marc]6-Jul-05 17:16 
AnswerRe: What about PrintPreviewDialog Pin
Prestaul27-Mar-06 10:42
Prestaul27-Mar-06 10:42 
QuestionIs there ANOTHER printing bug in the .NET framework? Pin
reflex@codeproject3-Jul-04 4:48
reflex@codeproject3-Jul-04 4:48 
AnswerRe: Is there ANOTHER printing bug in the .NET framework? Pin
webstorm10-Apr-07 7:38
webstorm10-Apr-07 7:38 
General2.0 Beta 1 framework Pin
nxtwothou2-Jul-04 5:34
nxtwothou2-Jul-04 5:34 
GeneralRe: 2.0 Beta 1 framework Pin
Philippe Leybaert3-Jul-04 3:02
Philippe Leybaert3-Jul-04 3:02 
GeneralRe: 2.0 Beta 1 framework Pin
Philippe Leybaert3-Jul-04 3:51
Philippe Leybaert3-Jul-04 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.