Determine Printer Margins Programmatically

The GetDeviceCaps

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

$SAMPLECODE$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

CLEAR

#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 

#DEFINE HORZRES       8     && the width, in pixels, of the printable area of the page
#DEFINE VERTRES       10    && the height, in pixels, of the printable area of the page

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)

? "Printer "  + lcPrinter

lnDC = CreateDC("", lcPrinter, 0, 0)

lnPixelsPerInchY = GetDeviceCaps(lnDC, LOGPIXELSY)
lnPixelsPerInchX = GetDeviceCaps(lnDC, LOGPIXELSX)

? "Pixels per Inch Y/X", lnPixelsPerInchY, lnPixelsPerInchX

lnPrinterMarginLeft = GetDeviceCaps(lnDC , PHYSICALOFFSETX)
lnPrinterMarginTop  = GetDeviceCaps(lnDC , PHYSICALOFFSETY)

*? "Margins Top/Left", lnPrinterMarginTop, lnPrinterMarginLeft

lnPrinterMarginLeftInch = lnPrinterMarginLeft / lnPixelsPerInchX
lnPrinterMarginTopInch  = lnPrinterMarginTop / lnPixelsPerInchY

? "Margins Top/Left", lnPrinterMarginTopInch, lnPrinterMarginLeftInch

lnPrintableAreaWidth = GetDeviceCaps(lnDC , HORZRES)
lnPrintableAreaHeight  = GetDeviceCaps(lnDC , VERTRES)

lnPrintableAreaWidthInch = lnPrintableAreaWidth / lnPixelsPerInchX 
lnPrintableAreaHeightInch = lnPrintableAreaHeight / lnPixelsPerInchY

? "Printable Area Height/Width", lnPrintableAreaHeightInch, lnPrintableAreaWidthInch 

lnPhysicalWidth = GetDeviceCaps(lnDC , PHYSICALWIDTH)
lnPhysicalHeight = GetDeviceCaps(lnDC , PHYSICALHEIGHT)

lnPhysicalWidthInch = lnPhysicalWidth / lnPixelsPerInchX 
lnPhysicalHeightInch = lnPhysicalHeight / lnPixelsPerInchY

? "Physical size Height/Width", lnPhysicalHeightInch, lnPhysicalWidthInch 

lnPrinterMarginBottomInch = lnPhysicalHeightInch - lnPrintableAreaHeightInch - lnPrinterMarginTopInch
lnPrinterMarginRightInch = lnPhysicalWidthInch - lnPrintableAreaWidthInch - lnPrinterMarginLeftInch

? "Margins Bottom/Right", lnPrinterMarginBottomInch, lnPrinterMarginRightInch

DeleteDC(lnDC)



Comments

How I can get the size of the printed area during borderless printing. Note: The printed area is larger than the paper surface.