Print Preview

Print Preview is a powerful concept which should be considered in most application programs which provide printed reports.  Briefly, the idea involves displaying a replica of a printed document on the screen before it is committed to printing on paper.  There are other related benefits available as well, such as the opportunity to save this replica report permanently to a disk file.  PowerBASIC offers a simple and straightforward method to create printed reports which can be previewed on the screen.

 

The algorithm implemented by PowerBASIC can be summarized:

  1. Select a printer to be used for the printed report using the XPRINT ATTACH statement.

XPRINT ATTACH {DEFAULT | PrinterName$} [,JobName$]
XPRINT ATTACH CHOOSE [USING Flags&] [,JobName$]

  1. Use the XPRINT PREVIEW statement to select a graphic target (a graphic bitmap or graphic window) for the preview display.  You may create a new graphic target, or reuse one which already exists.  The target is identified by the handle and ID given when it was created.  You can optionally specify a callback function which is called upon every execution of an XPRINT FORMFEED or XPRINT PREVIEW CLOSE.  This statement should immediately follow the XPRINT ATTACH.

XPRINT PREVIEW hWin, ID [, CALL xxx]

  1. At this point, all subsequent XPRINT output will be redirected to the graphic target.  All data will be adjusted in size and position to the specification of the graphic target.  It is best to use care to keep the proportions of the graphic page similar to the printer page to avoid distortion of the previewed report.

  2. When XPRINT PREVIEW CLOSE is executed, it signals that the previewed report is completed.  Redirection of XPRINT data is ended, and the XPRINT data stream is now sent to the original attached printer.

  3. Repeat the XPRINT statements to create the desired report on the attached printer.  Generally, these XPRINT statements are best placed in a subroutine which can be repeated with a single line of code.

 

A simplified PRINT PREVIEW:

GRAPHIC WINDOW NEW "Preview", 200, 100, 400, 550 TO h&

XPRINT ATTACH DEFAULT

XPRINT PREVIEW h&, 0

CALL PrintIt         ' print to the preview window

XPRINT PREVIEW CLOSE

CALL PrintIt         ' print to the host printer

XPRINT CLOSE

...

...

SUB PrintIt()

  XPRINT "This is a test of preview..."

  XPRINT ELLIPSE (300,300) - (500,400), %rgb_red

  XPRINT RENDER "xx.bmp", (300,500)-(500,700)

END SUB

 

XPRINT PREVIEW must be executed immediately after XPRINT ATTACH or an error 98 "XPrint Preview Error" will be generated at run time.  No XPRINT statements (other than the XPRINT$ function) may be executed between XPRINT ATTACH and XPRINT PREVIEW.

If you include the CallBack option, the callback procedure must be a simple SUB with no parameters and no return value.  It is called automatically by the XPRINT engine at the completion of each preview page (upon execution of XPRINT FORMFEED or XPRINT PREVIEW CLOSE. This Sub can perform all sorts of housekeeping help, such as copying the preview bitmap for separate storage, counting pages in the report, or most anything else needed by your program.  Copying the bitmap is important in multi-page reports as XPRINT FORMFEED erases the graphic target for preview of the next page.

 

See Also

Printing

Printing Commands

XPRINT Code Group