NextGrid & NextDBGrid








NextGrid .NET

NxPreview Quick Start

Related Articles:

  • Skill:
  • Version: 1.0.0
  • Author: Wim van der Vegt
  • Created:
  • Updated: 2008-04-26
NxPreview is a Print Preview dialog that will take care of several tasks involved in printing.

It provides a multipage canvas at full printer resolution that an application can draw upon. NxPreview supports drawing and printing in color if the printer does too.

With a single OnRender event both the preview and the printing are done at the printers full resulution, thus eliminating any scaling problems.

In this OnRender event the following common sequence is used to print:







Drawing is performed on a MetaCanvas that has the same dimensions as the selected printer. The following code from the demo draws the separate areas onto a page:

with TNxPreview(Sender) do
  with MetaCanvas do
    //Draw first page
    MoveTo(Body.Left, Body.Top);
    LineTo(Body.Right, Body.Bottom);
    MoveTo(Body.Left, Body.Bottom);
    LineTo(Body.Right, Body.Top);
    if pdHeader in PrintItems then
    TextOut(Header.Left + LeftMargin, Header.Top, 'Header');
    TextOut(Body.Left + 40 * CharWidth, Body.Top + CharHeight, 'Body');
    if pdFooter in PrintItems then
    TextOut(Footer.Left + LeftMargin, Footer.Bottom - CharHeight, 'Footer');
    if pdLeftMargin in PrintItems then
    for i := 1 to Length('Left Margin') do
      TextOut(Body.Left - LeftMargin + CharWidth, Body.Top + i * CharHeight,
        'Left Margin'[i]);

Beware when using code like above demo code that it's not allowed to use BeginDoc, NewPage or EndDoc inside a "with MetaCanvas do" block. These statements change the current MetaCanvas drawn upon (and referenced by the "with/do" statement) something Delphi doesn't like.

The output looks like the screenshot below.

In this screenshot the hatched area is the non-printable area as reported by the printer driver. Normally the application only draws within the Body rectangle. If present, the Header/Footer and Left Margin are automatically generated by the NxPreview component. If one or more of these areas are ommitted from the PrintItems property the Body is automatically adjusted so it includes the ommitted area too.

The NxPreview Dialog is shown by the following code:


The possible parameters for the Execute function are:

Table 1: Execute parameters
Name Meaning
poPreview Shows the preview dialog after calling the OnRender Event
poPrintSetup Shows the Print Setup Dialog
poReportSetup Fires the OnReportSetup event that the application can use to let the user alter whats printed.
poPrint Directly prints after firing the OnRender event.

Inside the NxPreview dialog, the OnReportSetup event is fired when the Report setup button is pressed. The button is disabled when no event is attached.

The OnJob event provides access to the Printing Jobs in the Spooler. It is called once for every Job when the Jobs property is retrieved.

With the runtime Settings property one can specify the name of an ini file where to store or load the most important printing settings. By default it points to a file based on the Applications name with an .ini extension.

For the headers and footers a number of opcodes are available that are replaced at runtime. Within the aplication the Parameter property can be used to define additional opcodes by using the "name=value" syntax of the TStringList class. Opcodes in the HeaderText and FooterText property must be enclosed in quare brackets.

Table 2: Header and footers opcodes
Name Meaning
[Page] The actual page number
[Date] Expanded to the output of DateToStr(Now)
[DateTime] Expanded to the output of DateTimeToStr(Now)
[Ldate] Expanded to the output of FormatDateTime('dddddd')
[Time] Expanded to the ouput of TimeToStr(Now)

Was This Article Useful?

Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.