Determine Printer Margins Programmatically

Tagged:

The GetDeviceCaps function can be used to retrieve printer-specific information as demonstrated below.

This is sample code. Add error handling and adjust to your requirements as necessary.

#define LOGPIXELSX    88    && Logical pixels/inch in X         
#define LOGPIXELSY    90    && Logical pixels/inch in Y         
 
#define PHYSICALWIDTH   110 && Physical Width in device units   
#define PHYSICALHEIGHT  111 && Physical Height in device units  
#define PHYSICALOFFSETX 112 && Physical Printable Area x margin 
#define PHYSICALOFFSETY 113 && Physical Printable Area y margin 
 
DECLARE Long GetDeviceCaps IN gdi32 Long hDC, Long nIndex
DECLARE Long CreateDC IN gdi32 String lpszDriver, String lpszDevice, Long lpszOutput, Long lpInitData
DECLARE Long DeleteDC IN gdi32 Long hDC
 
&& Use default printer
lcPrinter = SET("Printer",3)
 
lnDC = CreateDC("", lcPrinter, 0, 0)
 
lnPixelsPerInchY = GetDeviceCaps(lnDC, LOGPIXELSY)
lnPixelsPerInchX = GetDeviceCaps(lnDC, LOGPIXELSX)
 
? lnPixelsPerInchY, lnPixelsPerInchX
 
lnPrinterMarginLeft = GetDeviceCaps(lnDC , PHYSICALOFFSETX)
lnPrinterMarginTop  = GetDeviceCaps(lnDC , PHYSICALOFFSETY)
 
lnPrinterMarginLeftInch = lnPrinterMarginLeft / lnPixelsPerInchX
lnPrinterMarginTopInch  = lnPrinterMarginTop / lnPixelsPerInchY
 
? lnPrinterMarginTopInch, lnPrinterMarginLeftInch
 
DeleteDC(lnDC)