HOWTO: How to Use a Program to Calculate Print Margins (122037)



The information in this article applies to:

  • Microsoft Platform Software Development Kit (SDK) 1.0

This article was previously published under Q122037
4.00 | 3.10 3.50 3.51 WINDOWS | WINDOWS NT kbprint kbprg kbcode

SUMMARY

The Windows Software Development Kit (SDK) does not provide a function to calculate printer margins directly. An application can calculate this information using calls to the GetDeviceCaps(). This article discusses those functions and provides code fragments as illustrations.

MORE INFORMATION

An application can determine printer margins as follows:

Calculate the left and top margins
  1. Determine the upper left corner of the printable area calling GetDeviceCaps() with the PHYSICALOFFSETX and PHYSICALOFFSETY indices. For example:
       // Init our pt struct in case escape not supported
       pt.x = 0; pt.y = 0;
    
       // Locate the upper left corner of the printable area
       pt.x = GetDeviceCaps(hPrnDC, PHYSICALOFFSETX);
       pt.y = GetDeviceCaps(hPrnDC, PHYSICALOFFSETY);
    						
  2. Determine the number of pixels required to yield the desired margin (x and y offsets) by calling GetDeviceCaps() using the LOGPIXELSX and LOGPIXELSY flags.
       // Figure out how much you need to offset output to produce the left 
       // and top margins for the output in the printer. Note the
       // use of the "max" macro. It is possible that you are asking for
       // margins that are not possible on this printer. For example, the HP
       // LaserJet has a 0.25" unprintable area so we cannot get margins of
       // 0.1".
    
       xOffset = max (0, GetDeviceCaps (hPrnDC, LOGPIXELSX) *
                         nInchesWeWant - pt.x);
    
       yOffset = max (0, GetDeviceCaps (hPrnDC, LOGPIXELSY) *
                         nInchesWeWant - pt.y);
    
       // When doing all the output, you can either offset it by the above
       // values or call SetViewportOrg() to set the point (0,0) at
       // the margin offset you calculated.
    
       SetViewportOrg (hPrnDC, xOffset, yOffset);
       all other output here
    						
Calculate the bottom and right margins
  1. Obtain the total size of the physical page (including printable and unprintable areas)calling GetDeviceCaps() with the PHYSICALWIDTH and PHYSICALHEIGHT indices in Windows NT.
  2. Determine the number of pixels required to yield the desired right and bottom margins by calling GetDeviceCaps using the LOGPIXELSX and LOGPIXELSY flags.
  3. Calculate the size of the printable area with GetDeviceCaps() using the HORZRES and VERTRES flags. The following code fragment illustrates steps a through c:
       // Get the size of the printable area
       pt.x = GetDeviceCaps(hPrnDC, PHYSICALWIDTH);
       pt.y = GetDeviceCaps(hPrnDC, PHYSICALHEIGHT);
    
       xOffsetOfRightMargin = xOffset +
                              GetDeviceCaps (hPrnDC, HORZRES) -
                              pt.x -
                              GetDeviceCaps (hPrnDC, LOGPIXELSX) *
                              wInchesWeWant;
    
       yOffsetOfBottomMargin = yOffset +
                               GetDeviceCaps (hPrnDC, VERTRES) -
                               pt.y -
                               GetDeviceCaps (hPrnDC, LOGPIXELSY) *
                               wInchesWeWant;
    						
NOTE: Now, you can clip all output to the rectangle bounded by xOffset, yOffset, xOffsetOfRightMargin, and yOffsetOfBottomMargin.

Modification Type:MinorLast Reviewed:7/11/2005
Keywords:kbhowto KB122037