Example: Printing the Same Logical Page of Two Different Views

The following code prints a crosstab and a graph that display the same data. It prints the first logical page of the crosstab, and then the first logical page of the graph. Then it prints the second logical page of the crosstab, and then the second logical page of the graph. This continues until all of the crosstab and graph are printed. This example assumes that you have a crosstab named myCrosstab and a graph named myGraph.

You will find a complete sample that accomplishes this task in the oracle.dss.samples.printing package. The printing code in this example comes from the MultiViewPrintDialog class. The set up and property settings are modified here so that you can see more of the process in one routine. The complete example implements a dialog box and has a routine for setting properties, which are not replicated here.


// create an array of view printers ViewPrinter[] viewPrinters; viewPrinters[0] = new CrosstabPrinter(myCrosstab); viewPrinters[1] = new GraphPrinter(myGraph); // get a print job Frame frame = oracle.bali.ewt.util.WindowUtils.parentFrame(myCrosstab); PrintJob job = (java.awt.Toolkit.getDefaultToolkit()).getPrintJob(frame, "Print", null); // get a graphics object from the print job Graphics g = job.getGraphics(); // set properties on the view printers viewPrinters[0].setRepeatHeaders(true); viewPrinters[0].setPrintByColumns(true); HeaderAndFooterPainter cHeader = viewPrinters[0].getHeaderAndFooterPainter(ViewPrinter.HEADER_CENTER); cHeader.setContentType(HeaderAndFooterPainter.STRINGS); // create an array of Strings to display String[] headerText = new String[1]; headerText[0] = "Sales Crosstab"; cHeader.setStrings(headerText); HeaderAndFooterPainter footer; for(int i=0; i<viewPrinters.length;i++); { viewPrinters[i].setPrintScaleType(ViewPrinter.ORIGINAL_SIZE); // set to print all logical pages viewPrinters[i].setRangeType(ViewPrinter.ALL_LOGICAL_PAGES); footer = viewPrinters[i].getHeaderAndFooterPainter(ViewPrinter.FOOTER_LEFT); footer.setContentType(HeaderAndFooterPainter.PAGE_NUMBER); } // set up for printing for(int i=0; i<viewPrinters.length;i++) { // initialize each viewprinter viewPrinters[i].startPrint(); // set the same print job on each view printer, so that they // both print to a single print job viewPrinters[i].setPrintJob(job); // prepare the first page of each view viewPrinters[i].prepareFirstPage(g); } // end of preparation loop // print (this comes from oracle.dss.samples.printing.MultiViewPrintDialog) int nView = 0; while(true){ // print the current physical page viewPrinters[nView].renderPage(g); // if current page is last physical page in a logical page if(viewPrinters[nView].isLastPhysicalPage()) { // if this is not the last view, then go to the next view if(nView<(viewPrinters.length-1)) view++; // if this is the last view, then we have printed everything, so break else if(viewPrinters[nView].hasNextPage()==false) break; // otherwise, we are at the last view, but not the last page else { // so, go back to the first view nView = 0; // and prepare to print the next page of each view for(int i=0;i<viewPrinters.length;i++) // we normally would call hasNextPage, but because // this code is executed only if isLastPhysicalPage // is false, we know that there must be a next page viewPrinters[i].prepareNextPage(g); } // end of else (last view but not last page) } // end of if last physical page in logical page // otherwise, we have more physical pages to print in this logical page else // so prepare next physical page to print viewPrinters[nView].prepareNextPage(g); } // end of while loop // end the print job job.end(); // call endprint for each printer for(int i=0;i<viewPrinters.length;i++) viewPrinters[i].endPrint();