public class JRPrintServiceExporter extends JRAbstractExporter<PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput,JRExporterContext> implements Printable
java.awt.print.PrinterJob
java.awt.print.PrinterJob
javax.print.DocPrintJob
and a service-formatted javax.print.DocFlavor
javax.print.DocPrintJob
and a service-formatted javax.print.DocFlavor
JRPrintServiceExporter
implementation takes the first approach and uses some of the methods added to the
java.awt.print.PrinterJob
class:
PrintService
or
StreamPrintServiceFactory
objects depending on the methodPrintService
on a PrinterJob
pageDialog()
method that takes a PrintRequestAttributeSet
parameterprintDialog()()
method that takes a PrintRequestAttributeSet
parameterPrintRequestAttributeSet
parameterjavax.print.attribute.PrintServiceAttributeSet
object that is passed as the
value for the special
getPrintServiceAttributeSet()
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, a
javax.print.PrintService
instance can be passed in using the
getPrintService()
exporter configuration setting when users do not want the Java Print Service to search for an
available print service.
PrinterJob
instance.
Further customization is made by passing a
javax.print.attribute.PrintRequestAttributeSet
instance when calling the
print()
method on the PrinterJob
object to start the printing process.
To supply the javax.print.attribute.PrintRequestAttributeSet
object
containing the desired javax.print.attribute.PrintRequestAttribute
values to
the exporter, set the special
getPrintRequestAttributeSet()
exporter configuration setting.
isDisplayPageDialog()
and
isDisplayPrintDialog()
,
both receiving java.lang.Boolean
values, which show or
suppress the page dialog and/or the print dialog associated with the PrinterJob
instance.
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, if
isDisplayPageDialog()
or
isDisplayPrintDialog()
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()
and
isDisplayPrintDialogOnlyOnce()
. These
are only effective if the corresponding
isDisplayPageDialog()
or
isDisplayPrintDialog()
are
set to true.
If isDisplayPageDialogOnlyOnce()
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 when
isDisplayPrintDialogOnlyOnce()
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/printservice
sample:
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)); }
PrintServiceExporterConfiguration
Modifier and Type | Class and Description |
---|---|
protected class |
JRPrintServiceExporter.ExporterContext |
JRAbstractExporter.BaseExporterContext, JRAbstractExporter.PageRange
Modifier and Type | Field and Description |
---|---|
protected JRGraphics2DExporter |
exporter |
protected SimpleGraphics2DReportConfiguration |
grxConfiguration |
protected static String |
PRINT_SERVICE_EXPORTER_PROPERTIES_PREFIX |
protected int |
reportIndex |
allSelector, crtCompositeConfiguration, crtCompositeItemConfiguration, crtItem, dateFormatCache, EXCEPTION_MESSAGE_KEY_END_PAGE_INDEX_OUT_OF_RANGE, EXCEPTION_MESSAGE_KEY_PAGE_INDEX_OUT_OF_RANGE, EXCEPTION_MESSAGE_KEY_START_PAGE_INDEX_OUT_OF_RANGE, exporterConfiguration, exporterContext, exporterInput, exporterOutput, filter, itemConfiguration, jasperPrint, jasperReportsContext, noBackcolorSelector, noneSelector, numberFormatCache, parameters, propertiesUtil, PROPERTY_DEFAULT_FILTER_FACTORY, PROPERTY_SUFFIX_DEFAULT_FILTER_FACTORY, styledTextUtil
NO_SUCH_PAGE, PAGE_EXISTS
Constructor and Description |
---|
JRPrintServiceExporter() |
JRPrintServiceExporter(JasperReportsContext jasperReportsContext) |
Modifier and Type | Method and Description |
---|---|
static boolean |
checkAvailablePrinters() |
protected void |
ensureOutput() |
void |
exportReport()
Performs the export.
|
protected Class<PrintServiceExporterConfiguration> |
getConfigurationInterface() |
String |
getExporterKey() |
String |
getExporterPropertiesPrefix()
Returns the properties prefix for the current exporter.
|
protected Class<PrintServiceReportConfiguration> |
getItemConfigurationInterface() |
PrintService |
getPrintService()
Returns the
PrintService instance 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 void |
initExport() |
protected void |
initReport() |
int |
print(Graphics graphics,
PageFormat pageFormat,
int pageIndex) |
createFilter, defaultParseNumber, ensureInput, ensureJasperReportsContext, getBooleanCellValue, getCurrentConfiguration, getCurrentItemConfiguration, getCurrentJasperPrint, getCustomHandler, getDateCellValue, getDateFormat, getExporterContext, getExporterInput, getExporterOutput, getHyperlinkProducer, getJasperReportsContext, getLocale, getNumberCellValue, getNumberFormat, getOffsetX, getOffsetY, getPageRange, getParameter, getParameters, getPropertiesUtil, getReportContext, getStyledText, getStyledText, getTextFormatFactoryClass, getTextLocale, getTextTimeZone, getTextValue, getTextValueString, reset, resetClassLoader, resetExportContext, restoreElementOffsets, setClassLoader, setConfiguration, setConfiguration, setCurrentExporterInputItem, setExporterInput, setExporterOutput, setFrameElementsOffset, setJasperPrint, setJasperReportsContext, setOffset, setOffset, setParameter, setParameters, setReportContext
protected static final String PRINT_SERVICE_EXPORTER_PROPERTIES_PREFIX
protected JRGraphics2DExporter exporter
protected SimpleGraphics2DReportConfiguration grxConfiguration
protected int reportIndex
public JRPrintServiceExporter()
public JRPrintServiceExporter(JasperReportsContext jasperReportsContext)
protected Class<PrintServiceExporterConfiguration> getConfigurationInterface()
protected Class<PrintServiceReportConfiguration> getItemConfigurationInterface()
protected void ensureOutput()
public void exportReport() throws JRException
Exporter
exportReport
in interface Exporter<ExporterInput,PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput>
exportReport
in class JRAbstractExporter<PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput,JRExporterContext>
JRException
protected void initExport()
protected void initReport()
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
print
in interface Printable
PrinterException
public static boolean checkAvailablePrinters()
public Boolean[] getPrintStatus()
public PrintService getPrintService()
PrintService
instance used by the exporter last time the exportReport() method was run.public String getExporterKey()
public String getExporterPropertiesPrefix()
JRAbstractExporter
getExporterPropertiesPrefix
in class JRAbstractExporter<PrintServiceReportConfiguration,PrintServiceExporterConfiguration,ExporterOutput,JRExporterContext>
Copyright © 2014. All rights reserved.