![]()
Printing
ICEbrowser SDK provides a rich set of printing features for various content types. Some of the print features you can implement are as follows:
For a complete example of implemented print features, such as print setup, print preview, smart page breaks, headers and footers, and background image printing, run the Swing RI, as described in Swing RI.
Using the RICommon Printing Classes
The RICommon (ice.ri.common.print) package provides simplified printing procedures for various Java SDKs. You can quickly integrate printing into your application using the RICommon print classes, taking advantage of the StormPageFormat print features.
If you need to implement your own print routines, first review the printing source code available in the ice.ri.common.print package.
Using the StormPrinter Class
You can implement printing in your application independent of the Abstract Window Toolkit (AWT) printing system or any other printing API. The printing functions work with Java 1.1.8 and higher and are compatible with third-party APIs. Your code uses the StormPrinter class as follows:
StormPrinter stormPrinter = pilot.createPrinter();Each pilot's print renderer prints to the contents of a Java Graphics object. The print renderer formats the content to best fit the specified page format. You use the StormPageFormat class to store properties about the page format, such as margin width and height, page size, and page orientation.
Printing under Java 1.1.8
The following code sample shows how you can set up printing in Java 1.1.8. The source code is available under ICEbrowserExamples/src/printing/JavaPrinting.java.
To support printing with JDK 1.1.8, you can use the ice.ri.common.print.jdk11 library. If your application is being deployed on a later JDK, use the ice.ir.common.print.jdk13 library instead, as it offers richer printing support.
The "Print Selection" option is not enabled in the print dialog. To use this feature, set the StormPageFormat object's setPrintSelected() method to true.
The following code assumes that you have already set up the object stormPageFormat. This object is passed into the constructor of a PageSetupDialog, which allows the user to configure the stormPageFormat object using the provided GUI.
// Create and set up dialog Viewport view = stormBase.findViewportByName(viewportId); PageSetupDialog pageSetup = new PageSetupDialog(parentFrame, view, stormPageFormat); // show the dialog and store the altered stormPageFormat stormPageFormat = pageSetup.showDialog(); // Setup of the printer PrintHelper11 printHelper11 = new PrintHelper11(view, stormPageFormat); // Start the print job, where parentframe is an AWT Frame object printHelper11.print(parentFrame);Printing under Java 1.3
With JDK 1.3 and above, use the package ice.ri.common.print.jdk13, which allows the most flexible print configuration. You can set the JobAttributes to show a native, platform-independent dialog, or no dialog at all, when the print job starts. This package also respects any page attributes that are set when the print dialog is displayed, such as the number of copies, print selection, media size, and page ranges.
The following code shows how you can print rendered content.
// Create and set up dialog Viewport view = stormBase.findViewportByName(viewportId); PageSetupDialog pageSetup = new PageSetupDialog(parentFrame, view, stormPageFormat); // show the dialog and store the altered stormPageFormat stormPageFormat = pageSetup.showDialog(); // Create new job attributes JobAttributes jobAttributes = new JobAttributes(); // set the job to use the operating system native print dialog jobAttributes.setDialog(JobAttributes.DialogType.NATIVE); // Set up the printer PrintHelper13 printHelper13 = new PrintHelper13(view, stormPageFormat, jobAttributes); // Start the print job, where parentframe is a awt Frame object. printHelper11.print(parentFrame);Printing under Java 2
In some situations you may want to use the Java 2 print interfaces Printable and Pageable. The following code sample shows how you can set up printing in Java 2 using the StormPrinter. The example includes using the Java Print setup dialog box for editing properties in the StormPageFormat. The source code is available under ICEbrowserExamples/src/printing/Java2Printing.java.
// Create printer for current pilot Viewport viewport = stormBase.findViewportByName(viewportId); Pilot pilot = viewport.getPilot(); // create a new StormPrinter StormPrinter stormPrinter = pilot.createPrinter(); // create a new StormPageFormat with default properties StormPageFormat stormPageFormat = new StormPageFormat(); // add some page decoration to the printer stormPageFormat.addDecoration(new StringPageDecoration("URL", viewport.getLocation(), StringPageDecoration.TOP, StringPageDecoration.LEFT)); // set paper size stormPageFormat.setPageSize(StormPageFormat.NA_LETTER); // Set the zoom of the printer to fit the page width. double scale = (1.0 / stormPrinter.getDimension().width ) * stormPageFormat.getImageableWidth(); stormPageFormat.setScaleToFitEnabled(true); stormPageFormat.setScale(scale); // turn off background images stormPageFormat.setBackgroundVisible(false); // assign StormPageFormat to Storm printer at which time // the number of pages are calculated stormPrinter.setStormPageFormat(stormPageFormat); // start the print job PageableDocument document = new PageableDocument(stormPrinter); document.setPageDialogVisible(false); document.setPrintDialogVisible(false); document.print(); // post print clean up stormPrinter.dispose();Printing Pages with Multiple Frames in Java 2
The examples in the ice.ri.common.print package contain all the code you need to print multiple frames in JDK 1.1.8 and 1.3. However, in some situations you may want to print frames using the Java 2 print interfaces Printable and Pageable.
You can configure your application to print documents that include multiple frames. For an example, see the ICEbrowserExamples/src/printing/FramesetPrinting.java example code. The example shows how to print pages that have multiple frames in two ways:
- If the user selects one of the frames in the frameset, only the selected frame prints.
- If no individual frame is selected, all the frames print, each frame as a separate document.
Printing Multiple Pages
The StormPrinter interface uses the notion of a page count to describe the number of pages. The following example shows how to navigate through the pages in a print job:
// Print all pages. for(int i = 0; i < stormPrinter.getPageCount(); i++){ Graphics g = printJob.getGraphics(); stormPrinter.printPage(g, i); g.dispose(); }
|
Copyright 2005. ICEsoft Technologies, Inc. http://www.icesoft.com |