How to print a Canvas content to a Printer

Hi everybody,
I’m a part-time coder therefore I’m sure that I’m missing something but I really tried before sample codes and read the documentation.
I can write the routine to write into a Canvas graphic object but I wish have a button to print the content to a printer. I only found the solution to write twice all the lines to .DrawText, ones to fill the canvas and ones to print it out to a desktop printer. I’m sure there must be a way to get it simpler by cloning the graphics from one object to an other and draw it to the printer. Thank you

When you .drawtext to a canvas, you do that using the g as graphics parameter in the Paint event

To do the same on the printer,
create a method called DrawStuff (g as graphics)
and move all the drawing into that
Then in the paint event , call DrawStuff g instead

Now that you have a method to draw on graphics, create another method for printing.


    dim pag as graphics
    pag = OpenPrinterdialog(nil,self)  // this will prompt for a printer and return a graphics object
    DrawStuff pag   //this draws the same stuff on the printer
    pag = nil   // the page is ejected when the graphics goes out of context

Thank you a lot Jeff, that’s what I was looking for :wink: sometimes a little hint saves the day.

is for MacOS only and deprecated in 2019r2 so I used for MS Windows
pag = OpenPrinterdialog(nil) and it worked in 2023r3

final solution with Printer Dialog:

Dim pag As graphics
pag = OpenPrinterDialog(Nil)  // this will prompt for a printer and return a graphics object
DrawStuff pag   //this draws the same stuff on the printer
pag = Nil   // the page is ejected when the graphics goes out of context

direct print with NO Printer Dialog:

Var ps As New PrinterSetup
Var g As Graphics
g = PrinterSetup.OpenPrinter(ps)
If g <> Nil Then DrawStuff g

Project example with other canvas codes can be downloaded from here canvas2printer.7z

1 Like