Guest User

Lavieri

a guest
May 7th, 2010
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4.  
  5. import javax.print.PrintService;
  6. import javax.print.PrintServiceLookup;
  7. import javax.print.attribute.HashPrintRequestAttributeSet;
  8. import javax.print.attribute.HashPrintServiceAttributeSet;
  9. import javax.print.attribute.PrintRequestAttributeSet;
  10. import javax.print.attribute.PrintServiceAttributeSet;
  11. import javax.print.attribute.standard.Copies;
  12. import javax.print.attribute.standard.PrinterName;
  13.  
  14. import net.sf.jasperreports.engine.JREmptyDataSource;
  15. import net.sf.jasperreports.engine.JRException;
  16. import net.sf.jasperreports.engine.JRExporterParameter;
  17. import net.sf.jasperreports.engine.JasperCompileManager;
  18. import net.sf.jasperreports.engine.JasperFillManager;
  19. import net.sf.jasperreports.engine.JasperPrint;
  20. import net.sf.jasperreports.engine.JasperPrintManager;
  21. import net.sf.jasperreports.engine.JasperReport;
  22. import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
  23. import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
  24.  
  25. public class PrintApp {
  26.     public static void main(String[] args) throws Exception {
  27.         PrintApp app = new PrintApp();
  28.         app.testPrinting();
  29.     }
  30.  
  31.     private void testPrinting() throws JRException {
  32.        
  33.         PrintService impressora = PrintServiceLookup.lookupDefaultPrintService();
  34.        
  35.        
  36.         System.out.println("get report template");
  37.         InputStream templateAsStream = ClassLoader.getSystemResourceAsStream("printertest.jrxml");
  38.          
  39.         System.out.println("compile report");
  40.         JasperReport jasperReport = JasperCompileManager.compileReport(templateAsStream);
  41.          
  42.         // submit parameters
  43.         Map parameters = new HashMap();
  44.         parameters.put("param1", "jasper report ...");
  45.         parameters.put("param2", "...rules");
  46.          
  47.         System.out.println("fill the compiled template");
  48.         JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
  49.          
  50.         PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
  51.         PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
  52.          
  53.         //printServiceAttributeSet.add(MediaSizeName.ISO_A4);
  54.         printServiceAttributeSet.add(new PrinterName("Zebra105SL-1", null));
  55.         printRequestAttributeSet.add(new Copies(1));
  56.          
  57.         JRPrintServiceExporter exporter = new JRPrintServiceExporter();
  58.          
  59.         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  60.         exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
  61.         exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);
  62.         exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
  63.         exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
  64.          
  65.         System.out.println("export to printer");
  66.          
  67.         exporter.exportReport();
  68.          
  69.         // JasperPrintManager.printReport(jasperPrint, false);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment