Class JRPrintServiceExporter
- java.lang.Object
-
- net.sf.jasperreports.engine.JRAbstractExporter<PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput,JRExporterContext>
-
- net.sf.jasperreports.engine.export.JRPrintServiceExporter
-
- All Implemented Interfaces:
Printable,JRExporter<ExporterInput,PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput>,Exporter<ExporterInput,PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput>
public class JRPrintServiceExporter extends JRAbstractExporter<PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput,JRExporterContext> implements Printable
Prints a JasperReports document using the Java Print Service API. There are four ways of using the Java Print Service with the Java 2D API:- Printing 2D graphics using
java.awt.print.PrinterJob - Streaming 2D graphics using
java.awt.print.PrinterJob - Printing 2D graphics using
javax.print.DocPrintJoband a service-formattedjavax.print.DocFlavor - Streaming 2D graphics using
javax.print.DocPrintJoband a service-formattedjavax.print.DocFlavor
JRPrintServiceExporterimplementation takes the first approach and uses some of the methods added to thejava.awt.print.PrinterJobclass:- Static convenience methods to look up print services that can image 2D graphics,
which are returned as an array of
PrintServiceorStreamPrintServiceFactoryobjects depending on the method - Methods to set and get a
PrintServiceon aPrinterJob - A
pageDialog()method that takes aPrintRequestAttributeSetparameter - A
printDialog()()method that takes aPrintRequestAttributeSetparameter - A print method that takes a
PrintRequestAttributeSetparameter
Looking Up a Printing Service
This exporter tries to find a print service that supports the necessary attributes. The set of attributes can be supplied to the exporter in the form of ajavax.print.attribute.PrintServiceAttributeSetobject that is passed as the value for the specialgetPrintServiceAttributeSet()exporter configuration setting. For more details about the attributes that can be part of such an attribute set, check the Java Print Service API documentation. The lookup procedure might return one or more print services able to handle the specified print service attributes. If so, the exporter uses the first one in the list. If no suitable print service is found, then the exporter throws an exception. As an alternative, ajavax.print.PrintServiceinstance can be passed in using thegetPrintService()exporter configuration setting when users do not want the Java Print Service to search for an available print service.Configuring the Printer Job
Once a print service has been located, it is associated with aPrinterJobinstance. Further customization is made by passing ajavax.print.attribute.PrintRequestAttributeSetinstance when calling theprint()method on thePrinterJobobject to start the printing process. To supply thejavax.print.attribute.PrintRequestAttributeSetobject containing the desiredjavax.print.attribute.PrintRequestAttributevalues to the exporter, set the specialgetPrintRequestAttributeSet()exporter configuration setting.Displaying Print Dialogs
If this exporter is invoked by a desktop or client-side Java application, you can offer the end user a final chance to customize the printer job before the printing process actually starts. The exporter has two other predefined configuration settings:isDisplayPageDialog()andisDisplayPrintDialog(), both receivingjava.lang.Booleanvalues, which show or suppress the page dialog and/or the print dialog associated with thePrinterJobinstance. The two dialogs are cross-platform. They enable users to alter the print service attributes and the print request attributes that are already set for the current print service and printer job. They also allow canceling the current printing procedure altogether. When batch printing a set of documents, ifisDisplayPageDialog()orisDisplayPrintDialog()are set to true, a dialog window will pop up each time a document in the list is to be printed. This is very useful if you intend to set different printing options for each document. However, setting the same page/printing options each time would quickly become cumbersome. If same settings are intended for all documents in the list, the exporter provides two additional predefined export configuration settings:isDisplayPageDialogOnlyOnce()andisDisplayPrintDialogOnlyOnce(). These are only effective if the correspondingisDisplayPageDialog()orisDisplayPrintDialog()are set to true. IfisDisplayPageDialogOnlyOnce()is true, then the page dialog will open only once, and the export options set within will be preserved for all documents in the list. The same thing happens whenisDisplayPrintDialogOnlyOnce()is set to true - the print dialog will open only once. Below is an example of configuring the print service exporter taken from the supplied/demo/ samples/printservicesample:public void print() throws JRException { PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(MediaSizeName.ISO_A4); PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet(); //printServiceAttributeSet.add(new PrinterName("Epson Stylus 820 ESC/P 2", null)); //printServiceAttributeSet.add(new PrinterName("hp LaserJet 1320 PCL 6", null)); //printServiceAttributeSet.add(new PrinterName("PDFCreator", null)); JRPrintServiceExporter exporter = new JRPrintServiceExporter(); exporter.setExporterInput(new SimpleExporterInput("build/reports/PrintServiceReport.jrprint")); SimplePrintServiceExporterConfiguration configuration = new SimplePrintServiceExporterConfiguration(); configuration.setPrintRequestAttributeSet(printRequestAttributeSet); configuration.setPrintServiceAttributeSet(printServiceAttributeSet); configuration.setDisplayPageDialog(false); configuration.setDisplayPrintDialog(true); exporter.setConfiguration(configuration); exporter.exportReport(); System.err.println("Printing time : " + (System.currentTimeMillis() - start)); }- Author:
- Teodor Danciu (teodord@users.sourceforge.net)
- See Also:
PrintServiceExporterConfiguration
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classJRPrintServiceExporter.ExporterContext-
Nested classes/interfaces inherited from class net.sf.jasperreports.engine.JRAbstractExporter
JRAbstractExporter.BaseExporterContext, JRAbstractExporter.PageRange
-
-
Field Summary
Fields Modifier and Type Field Description static StringEXCEPTION_MESSAGE_KEY_PRINT_SERVICE_NOT_FOUNDprotected JRGraphics2DExporterexporterprotected SimpleGraphics2DReportConfigurationgrxConfigurationprotected static StringPRINT_SERVICE_EXPORTER_PROPERTIES_PREFIXprotected intreportIndex-
Fields inherited from class net.sf.jasperreports.engine.JRAbstractExporter
allSelector, crtCompositeConfiguration, crtCompositeItemConfiguration, crtItem, dateFormatCache, EXCEPTION_MESSAGE_KEY_END_PAGE_INDEX_OUT_OF_RANGE, EXCEPTION_MESSAGE_KEY_INVALID_IMAGE_NAME, EXCEPTION_MESSAGE_KEY_INVALID_ZOOM_RATIO, EXCEPTION_MESSAGE_KEY_MIXED_CALLS_NOT_ALLOWED, EXCEPTION_MESSAGE_KEY_OUTPUT_WRITER_ERROR, EXCEPTION_MESSAGE_KEY_PAGE_INDEX_OUT_OF_RANGE, EXCEPTION_MESSAGE_KEY_START_PAGE_INDEX_OUT_OF_RANGE, exporterConfiguration, exporterContext, exporterInput, exporterOutput, filter, fontUtil, hyperlinkProducerFactory, itemConfiguration, jasperPrint, jasperReportsContext, noBackcolorSelector, noneSelector, numberFormatCache, parameters, propertiesUtil, PROPERTY_DEFAULT_FILTER_FACTORY, PROPERTY_SUFFIX_DEFAULT_FILTER_FACTORY, rendererUtil, styledTextUtil, textValueClasses
-
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
-
-
Constructor Summary
Constructors Constructor Description JRPrintServiceExporter()JRPrintServiceExporter(JasperReportsContext jasperReportsContext)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static booleancheckAvailablePrinters()protected voidensureOutput()voidexportReport()Performs the export.protected Class<PrintServiceExporterConfiguration>getConfigurationInterface()StringgetExporterKey()StringgetExporterPropertiesPrefix()Returns the properties prefix for the current exporter.protected Class<PrintServiceReportConfiguration>getItemConfigurationInterface()PrintServicegetPrintService()Returns thePrintServiceinstance used by the exporter last time the exportReport() method was run.Boolean[]getPrintStatus()Returns an array of java.lang.Boolean values, one for each appearance of the print dialog during the last export operation.protected voidinitExport()protected voidinitReport()intprint(Graphics graphics, PageFormat pageFormat, int pageIndex)-
Methods inherited from class net.sf.jasperreports.engine.JRAbstractExporter
checkInterrupted, createFilter, defaultParseNumber, ensureInput, ensureJasperReportsContext, getBooleanCellValue, getCurrentConfiguration, getCurrentItemConfiguration, getCurrentJasperPrint, getDateCellValue, getDateFormat, getExporterContext, getExporterInput, getExporterOutput, getHyperlinkProducer, getJasperReportsContext, getLocale, getNumberCellValue, getNumberFormat, getOffsetX, getOffsetY, getPageRange, getParameter, getParameters, getPropertiesUtil, getRendererUtil, getReportContext, getRepository, getStyledText, getStyledText, getTextFormatFactoryClass, getTextLocale, getTextTimeZone, getTextValue, getTextValueString, insideFrame, reset, resetClassLoader, resetExportContext, restoreElementOffsets, setClassLoader, setConfiguration, setConfiguration, setCurrentExporterInputItem, setExporterInput, setExporterOutput, setFrameElementsOffset, setJasperReportsContext, setOffset, setOffset, setParameter, setParameters, setReportContext
-
-
-
-
Field Detail
-
PRINT_SERVICE_EXPORTER_PROPERTIES_PREFIX
protected static final String PRINT_SERVICE_EXPORTER_PROPERTIES_PREFIX
- See Also:
- Constant Field Values
-
EXCEPTION_MESSAGE_KEY_PRINT_SERVICE_NOT_FOUND
public static final String EXCEPTION_MESSAGE_KEY_PRINT_SERVICE_NOT_FOUND
- See Also:
- Constant Field Values
-
exporter
protected JRGraphics2DExporter exporter
-
grxConfiguration
protected SimpleGraphics2DReportConfiguration grxConfiguration
-
reportIndex
protected int reportIndex
-
-
Constructor Detail
-
JRPrintServiceExporter
public JRPrintServiceExporter()
-
JRPrintServiceExporter
public JRPrintServiceExporter(JasperReportsContext jasperReportsContext)
-
-
Method Detail
-
getConfigurationInterface
protected Class<PrintServiceExporterConfiguration> getConfigurationInterface()
-
getItemConfigurationInterface
protected Class<PrintServiceReportConfiguration> getItemConfigurationInterface()
-
ensureOutput
protected void ensureOutput()
- Specified by:
ensureOutputin classJRAbstractExporter<PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput,JRExporterContext>
-
exportReport
public void exportReport() throws JRExceptionDescription copied from interface:ExporterPerforms the export.- Specified by:
exportReportin interfaceExporter<ExporterInput,PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput>- Specified by:
exportReportin classJRAbstractExporter<PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput,JRExporterContext>- Throws:
JRException
-
initExport
protected void initExport()
-
initReport
protected void initReport()
-
print
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
- Specified by:
printin interfacePrintable- Throws:
PrinterException
-
checkAvailablePrinters
public static boolean checkAvailablePrinters()
-
getPrintStatus
public Boolean[] getPrintStatus()
Returns an array of java.lang.Boolean values, one for each appearance of the print dialog during the last export operation. A Boolean.TRUE value in this array means that for that particular occurrence of the print dialog, the OK button was hit. A Boolean.FALSE value means the respective print dialog was canceled.
-
getPrintService
public PrintService getPrintService()
Returns thePrintServiceinstance used by the exporter last time the exportReport() method was run.
-
getExporterKey
public String getExporterKey()
- Specified by:
getExporterKeyin classJRAbstractExporter<PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput,JRExporterContext>
-
getExporterPropertiesPrefix
public String getExporterPropertiesPrefix()
Description copied from class:JRAbstractExporterReturns the properties prefix for the current exporter.- Specified by:
getExporterPropertiesPrefixin classJRAbstractExporter<PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput,JRExporterContext>- Returns:
- the properties prefix for the current exporter
-
-