REALbasic Printing Tutorial – Graphics Object

Introduction

REALbasic has made drawing really easy with its graphics object.  The graphics object is used when drawing into a Canvas object as well as a Printer object.

This method is the most difficult because you have to do everything and I mean everything (though it is easier than explaining the Theory of Relativity).  Drawing a single page is easy.  What about page 2 or page 3 or 4?  Do you even know how many pages you have in your report?  In the case of a database application you may never know how many pages your report will be until you actually run the report.

The other big question is do you show a preview or just print direct to the printer?  Most users these days expect a preview of their report before sending it to the printer.  Darn those programmers before us by spoiling users!

So why did we call this the Graphic Object method?  When we draw into the canvas we’re really drawing into a graphics object.  You’ll notice that the Paint method of the canvas object passes in a graphics object.  It just so happens that you get another graphics object when you print, hence the name of this method.

The Project

Download Project File: 

GraphicsPrinting (183.0 KiB, 722 downloads)

Take a look at the project called Graphic Object Method.  In Window1 we’re going to show our report as close as we can to how it’s going to look on the printed page.  We place a canvas control on the window and size it to our window.  We place locks on the canvas so it resizes with our window.

Because we’re in preview mode and our canvas probably won’t be the exact size of our printed page we’re going to draw a grey background and then a white page on top of it.  This will require some centering of rectangles after you’ve determined the size of the report versus the size of the canvas.  All this just to simulate the page on the screen!

The sample project isn’t really complete, but it’s enough to show you that this is not fun at all.  We haven’t even tried to integrate graphics.  We haven’t even put proper footers and page headers in it yet and we’ve already got a boatload of code.  We haven’t even done anything really fun with the text like use background colors.  Can you imagine the nightmares of putting in subtotals and grand totals or we wanted to group our data so the Categories were only printed once (or at the top of a page if we crossed pages)?

Conclusions

Perhaps the nicest thing about using the canvas to print is that you, the programmer, control every single aspect of printing from font, font size, locations and graphics and everything in between.  This is also this methods greatest weakness.

This method isn’t really object oriented code and not very reusable.  Essentially you’ll end up creating a lot of code to handle each report your application will have.  Imagine, if you will, an accounting program that could potentially have hundreds of reports all user configurable with optional fields and sum and totals.

There’s gotta be a better solution!

If you feel that you would like to contribute to these articles or have comments or suggestions, please use our forum.