Example: Printing a Crosstab and a Button on a Page

The following example shows how to print a crosstab, while printing a button at the top of each physical page on which the crosstab is printed, using the renderPage method and AWT printing. This example assumes that there is a button named myButton and a crosstab named myCrosstab.


CrosstabPrinter printer = new CrosstabPrinter(myCrosstab); Frame frame = oracle.bali.ewt.util.WindowUtils.parentFrame(myCrosstab); PrintJob job = (java.awt.Toolkit.getDefaultToolkit()).getPrintJob(frame, "Print", null); // find the size of the paper Dimension pageDim = job.getPageDimension(); // find the height of the button int buttonHeight = myButton.getSize().height; // subtract, to find the height available for the crosstab Dimension crosstabDim = new Dimension(pageDim.width, pageDim.height - buttonHeight // tell the crosstab printer how much space is available for the crosstab printer.setViewDimension(crosstabDim); // set properties on the printer here, before calling startPrint printer.setRangeType(ViewPrinter.ALL_LOGICAL_PAGES); if (printer.startPrint()) { Graphics g = job.getGraphics(); // print the button myButton.print(g); // reuse the Graphics object g.translate(0, buttonHeight); // go to the first physical page if (printer.prepareFirstPage(g)){ // print the first physical page of the crosstab printer.renderPage(g); // dispose of the Graphics object g.dispose(); // do the same thing as long as there are physical pages to print while(printer.hasNextPage()) { Graphics g = job.getGraphics(); myButton.print(g); g.translate(0, buttonHeight); if (printer.prepareNextPage(g)) printer.renderPage(g); } // end while } // end if prepare first page // end the print job printer.endPrint(); job.end(); } // end if startPrint