Java2D Based Print Previewer

It is often desirable to allow users to preview what is going to be printed before actually sending it to the printer. The previewing capability has become standard on most applications. Style Intelligence has built-in support for easily adding previewing to an application. A Java2 based previewer provides support for Java2 printing API.


BI DemoRegister
dashboard example

PreviewView Interface

The PreviewView interface defines the complete API for the previewer classes. Try not to use the previewer classes directly. All preview-related functions should be accessed through the PreviewView interface.

The Java2D API introduced in Java2 added another set of classes for performing printing. To support this new printing API in Java2, we use the inetsoft.report.j2d package, Previewer2D. The Previewer2D class is Swing based. It fully implements the API defined in the PreviewView class.

Creating a Previewer

A previewer can be created in a number of ways. The preferred method is through the Previewer.createPreviewer() method.

Adding previewing support to an application is as simple as creating a previewer and calling the print() method of the previewer to show a report.

 ReportSheet report = createReport();
 PreviewView previewer = Previewer.createPreviewer();
 previewer.print(report); 

Once a previewer is created and populated, simply show the window as any other window.

 previewer.pack(); previewer.setVisible(true); 

Scaling the Preview

By default, when a previewer is first displayed, it displays the pages in 100% view. No scaling is performed. A program can programmatically control the zooming of the preview window by calling the zoom() method.

 // scale to fit the entire page in window
 previewer.zoom(PreviewPane.WHOLE_PAGE); 

Adding a Preview Panel

A PreviewPane can be used to create a panel that can be added to other containers. It contains the area where the pages are shown, excluding the toolbar. It can be used in ways similar to the Previewer but cannot be shown by itself.

 PreviewPane preview = new PreviewPane();
 preview.print(report);
 add(preview, "Center"); 

Example: Print Preview
In the next example, we change the previous example report so that it provides a preview instead of directly printing.

Listing 8. Printing with Preview (Report2.java)

Class Report2 extends Frame {
   ...
   Button printB = new Button("Preview");
   pnl.add(printB);
   printB.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
         ReportSheet report = createReport();
         PreviewView previewer =
            Previewer.createPreviewer();
         previewer.pack();
         previewer.setVisible(true);
          previewer.print(report);
      }
   });
   ...
   public static void main(String[] args) {
     ReportEnv.setProperty("StyleReport.useCustomDriver",
             "true");
        Report2 win = new Report2();
        win.pack();
        win.setVisible(true);
   }
}

Note that the only difference between Report1 and Report2 is the printing action handler. To an application, the previewer is similar to a printer. The Previewer manages the entire previewing action and no additional code is necessary in the application.