PrinterSetup.OpenPrinter

From Xojo Documentation

Method

OpenPrinter(setup as PrinterSetup) As Graphics

New in 2019r2

Supported for all project types and targets.

Notes

The OpenPrinter function returns a Graphics object. The various drawing routines can then be used to draw into the Graphics object and will be sent to the printer for printing.

Although the pageSetup parameter is optional, in order to get the correct horizontal and vertical resolutions for drawing and positioning you will need to provide a PrinterSetup. You can do this without prompting the user with a dialog like this:

Var p As New PageSetup
g = PrinterSetup.OpenPrinter(p)
Var horizontalDPI = p.HorizontalResolution
Var verticalDPI = p.VerticalResolution

Sample Code

This code prints "Hello World" to the currently selected printer:

Var p As New PageSetup
Var g As Graphics
g = PrinterSetup.OpenPrinter(p)
If g <> Nil Then
// Draw text 1 inch across and 1 inch down
g.DrawString("Hello World", p.HorizontalResolution, p.VerticalResolution)
End If

This code uses the Page Setup properties for the printer chosen by the user by the PageSetup dialog:

Var ps As New PrinterSetup
If ps.ShowPageSetupDialog Then
Var g As Graphics
g = PrinterSetup.OpenPrinter(ps)
If g <> Nil Then
// Draw text 1 inch across and 1 inch down
g.DrawString("Hello World", ps.HorizontalResolution, ps.VerticalResolution)
End If
End If

See Also

Graphics, PrinterSetup classes; PrinterSetup.ShowPrinterDialog function.