Exemplo n.º 1
0
  public static void exporterPapier(JasperPrint jPrint, Integer nbCopies) {

    // choix de l'imprimante par défaut
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    // paramètres d'impression
    PrintRequestAttributeSet printRequestAttributeSetImpression =
        new HashPrintRequestAttributeSet();
    printRequestAttributeSetImpression.add(MediaSizeName.ISO_A4);
    printRequestAttributeSetImpression.add(new Copies(nbCopies));

    // paramètres de l'imprimante
    PrintServiceAttributeSet printServiceAttributeSetImpression =
        new HashPrintServiceAttributeSet();
    printServiceAttributeSetImpression.add(new PrinterName(service.getName(), null));

    // création de l'interface d'impression
    JRPrintServiceExporter imprimanteFacture = new JRPrintServiceExporter();

    // initialisation de l'interface d'impression
    imprimanteFacture.setParameter(JRExporterParameter.JASPER_PRINT, jPrint);
    imprimanteFacture.setParameter(
        JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,
        printRequestAttributeSetImpression);
    imprimanteFacture.setParameter(
        JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
        printServiceAttributeSetImpression);

    try {
      imprimanteFacture.exportReport();
    } catch (Exception e) {
      throw new RuntimeException("Impossible de générer la Facture", e);
      //  COMMANDE ENREGISTREE MAIS NON IMPRIMEE
    }
  }
Exemplo n.º 2
0
  /**
   * Takes a printer name and find the associated PrintService. If no match can be made it randomly
   * picks the first printer listed from the call to lookupPrintServices.
   *
   * @param printerName
   * @return PrintService referenced by the printerName
   */
  public PrintService getPrinterInternal(final String printerName, final String lastPrinterName) {
    // The parameter value was not provided, and we are allowed to create
    // user interface forms

    PrintService[] services = PrinterJob.lookupPrintServices();
    for (PrintService element : services) {
      if (element.getName().equals(printerName)) {
        return element;
      }
    }
    if (feedbackAllowed()) {
      // If it's not valid then lets find one and end this current run.
      ArrayList values = new ArrayList();
      for (PrintService element : services) {
        String value = element.getName();
        values.add(value);
      }
      createFeedbackParameter(
          StandardSettings.PRINTER_NAME,
          Messages.getInstance().getString("PrintComponent.USER_PRINTER_NAME"),
          "",
          lastPrinterName,
          values,
          null,
          "select"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      promptNeeded();
      return null;
    }
    return services[0];
  }
Exemplo n.º 3
0
  private void printDoc() {
    try {
      PrintService printService = null;
      PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
      for (PrintService ps : printServices) {
        if (ps.getName().equals(printerName)) {
          printService = ps;
          break;
        }
      }

      if (printService == null) {
        return;
      }

      PrinterJob job = PrinterJob.getPrinterJob();
      job.setPrintService(printService);
      CustomPaper customPaper = new CustomPaper(pageWidth, pageHeight);
      customPaper.setPrintArea(printAreaTop, printAreaLeft, printAreaWidth, printAreaHeight);

      PageFormat pf = job.defaultPage();
      pf.setPaper(customPaper);
      job.defaultPage(pf);
      job.setPrintable(this, pf);
      job.print();

    } catch (PrinterException | HeadlessException e) {
      System.err.println(e);
      Logger.getGlobal().log(Level.SEVERE, null, e);
    }
  }
Exemplo n.º 4
0
 public void pageableJob(Pageable pageable) throws PrintException {
   try {
     synchronized (this) {
       if (job != null) { // shouldn't happen
         throw new PrintException("already printing");
       } else {
         job = new sun.awt.windows.WPrinterJob();
       }
     }
     PrintService svc = getPrintService();
     job.setPrintService(svc);
     if (copies == 0) {
       Copies c = (Copies) svc.getDefaultAttributeValue(Copies.class);
       copies = c.getValue();
     }
     job.setCopies(copies);
     job.setJobName(jobName);
     job.setPageable(pageable);
     job.print(reqAttrSet);
     notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
     return;
   } catch (PrinterException pe) {
     notifyEvent(PrintJobEvent.JOB_FAILED);
     throw new PrintException(pe);
   } finally {
     printReturned = true;
     notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
   }
 }
  private synchronized void refreshServices() {
    printers = peer.getAllPrinterNames();
    if (printers == null) {
      // In Windows it is safe to assume no default if printers == null so we
      // don't get the default.
      printServices = new PrintService[0];
      return;
    }

    PrintService[] newServices = new PrintService[printers.length];
    PrintService defService = getDefaultPrintService();
    for (int p = 0; p < printers.length; p++) {
      if (defService != null && printers[p].equals(defService.getName())) {
        newServices[p] = defService;
      } else {
        if (printServices == null) {
          newServices[p] = new Win32PrintService(printers[p], peer);
        } else {
          int j;
          for (j = 0; j < printServices.length; j++) {
            if ((printServices[j] != null) && (printers[p].equals(printServices[j].getName()))) {
              newServices[p] = printServices[j];
              printServices[j] = null;
              break;
            }
          }
          if (j == printServices.length) {
            newServices[p] = new Win32PrintService(printers[p], peer);
          }
        }
      }
    }

    printServices = newServices;
  }
  // for retreiving all the printer name
  public String[] getAllPrinterName() {

    PrintService[] MetaPrinter = PrintServiceLookup.lookupPrintServices(null, null);
    ArrayList<String> PrinterNameStack = new ArrayList<>();
    // PrintService DefaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
    for (PrintService printer : MetaPrinter) {
      String printname = printer.getName();
      PrinterNameStack.add(printname);
    }
    return PrinterNameStack.toArray(new String[PrinterNameStack.size()]);
  }
Exemplo n.º 7
0
  public void printableJob(Printable printable) throws PrintException {
    try {
      synchronized (this) {
        if (job != null) { // shouldn't happen
          throw new PrintException("already printing");
        } else {
          job = new sun.awt.windows.WPrinterJob();
        }
      }
      PrintService svc = getPrintService();
      job.setPrintService(svc);
      if (copies == 0) {
        Copies c = (Copies) svc.getDefaultAttributeValue(Copies.class);
        copies = c.getValue();
      }

      if (mediaName == null) {
        Object media = svc.getDefaultAttributeValue(Media.class);
        if (media instanceof MediaSizeName) {
          mediaName = (MediaSizeName) media;
          mediaSize = MediaSize.getMediaSizeForName(mediaName);
        }
      }

      if (orient == null) {
        orient = (OrientationRequested) svc.getDefaultAttributeValue(OrientationRequested.class);
      }

      job.setCopies(copies);
      job.setJobName(jobName);
      PageFormat pf = new PageFormat();
      if (mediaSize != null) {
        Paper p = new Paper();
        p.setSize(mediaSize.getX(MediaSize.INCH) * 72.0, mediaSize.getY(MediaSize.INCH) * 72.0);
        p.setImageableArea(72.0, 72.0, p.getWidth() - 144.0, p.getHeight() - 144.0);
        pf.setPaper(p);
      }
      if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
        pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
      } else if (orient == OrientationRequested.LANDSCAPE) {
        pf.setOrientation(PageFormat.LANDSCAPE);
      }
      job.setPrintable(printable, pf);
      job.print(reqAttrSet);
      notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
      return;
    } catch (PrinterException pe) {
      notifyEvent(PrintJobEvent.JOB_FAILED);
      throw new PrintException(pe);
    } finally {
      printReturned = true;
      notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
    }
  }
Exemplo n.º 8
0
  /**
   * Takes a printer name and find the associated PrintService. If no match can be made it randomly
   * picks the first printer listed from the call to lookupPrintServices.
   *
   * @param printerName
   * @return PrintService referenced by the printerName
   */
  public static PrintService getPrinter(final String printerName) {
    // The parameter value was not provided, and we are allowed to create
    // user interface forms

    PrintService[] services = PrinterJob.lookupPrintServices();
    for (PrintService element : services) {
      if (element.getName().equals(printerName)) {
        return element;
      }
    }
    return services[0];
  }
Exemplo n.º 9
0
 @Cached
 /* package */ PrintService getSystemPrintService(final String printerName) {
   final PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
   if (services == null || services.length == 0) {
     return null;
   }
   for (final PrintService service : services) {
     if (service.getName().equals(printerName)) {
       return service;
     }
   }
   return null;
 }
Exemplo n.º 10
0
 public Set<PrintResolution> supportedPrintResolution() {
   if (resSet != null) {
     return resSet;
   }
   Set<PrintResolution> rSet =
       new TreeSet<PrintResolution>(PrintResolutionComparator.theComparator);
   PrinterResolution[] pr = null;
   try {
     pr =
         (PrinterResolution[])
             service.getSupportedAttributeValues(PrinterResolution.class, null, null);
   } catch (Exception e) {
   }
   if (pr == null || pr.length == 0) {
     rSet.add(defaultPrintResolution());
   } else {
     for (int i = 0; i < pr.length; i++) {
       int cfr = pr[i].getCrossFeedResolution(ResolutionSyntax.DPI);
       int fr = pr[i].getFeedResolution(ResolutionSyntax.DPI);
       rSet.add(PrintHelper.createPrintResolution(cfr, fr));
     }
   }
   resSet = Collections.unmodifiableSet(rSet);
   return resSet;
 }
Exemplo n.º 11
0
 public Set<PrintSides> supportedSides() {
   if (sidesSet == null) {
     Set<PrintSides> sSet = new TreeSet<PrintSides>();
     Sides[] ss = null;
     try {
       ss = (Sides[]) service.getSupportedAttributeValues(Sides.class, null, null);
     } catch (Exception e) {
     }
     if (ss != null) {
       for (int i = 0; i < ss.length; i++) {
         if (ss[i] == Sides.ONE_SIDED) {
           sSet.add(PrintSides.ONE_SIDED);
         }
         if (ss[i] == Sides.DUPLEX) {
           sSet.add(PrintSides.DUPLEX);
         }
         if (ss[i] == Sides.TUMBLE) {
           sSet.add(PrintSides.TUMBLE);
         }
       }
     }
     sidesSet = Collections.unmodifiableSet(sSet);
   }
   return sidesSet;
 }
  @Override
  public void startConsumeTask(Map<String, Object> printServices) {
    // 获取所有打印服务
    PrintService[] tempPrintServices = new PrintUtils().getAllPrintService();
    List<PrintService> printServiceList = new ArrayList<PrintService>();

    // 获取用户选择的打印服务对象
    for (PrintService printService : tempPrintServices) {
      if (printServices.containsKey(printService.getName())) {
        printServiceList.add(printService);
      }
    }

    // 处理任务
    scheduler.startConsumeTask(printServiceList);
  }
Exemplo n.º 13
0
  private void populateMedia() {
    initPrefinedMediaMaps();

    if (paperSet != null) {
      return; // already inited
    }
    Media[] media = (Media[]) service.getSupportedAttributeValues(Media.class, null, null);
    Set<Paper> pSet = new TreeSet<Paper>(PaperComparator.theComparator);
    Set<PaperSource> tSet = new TreeSet<PaperSource>(PaperSourceComparator.theComparator);
    /* We will get back a list of Media and want to look for
     * MediaSizeName and MediaTray instances and map to FX classes.
     * We will hard code here recognising the set we've chosen to
     * expose in FX API.
     * For the rest we'll need to create custom instances.
     */

    if (media != null) {
      for (int i = 0; i < media.length; i++) {
        Media m = media[i];
        if (m instanceof MediaSizeName) {
          pSet.add(addPaper(((MediaSizeName) m)));
        } else if (m instanceof MediaTray) {
          tSet.add(addPaperSource((MediaTray) m));
        }
      }
    }
    paperSet = Collections.unmodifiableSet(pSet);
    paperSourceSet = Collections.unmodifiableSet(tSet);
  }
Exemplo n.º 14
0
  public Set<PageOrientation> supportedOrientation() {
    if (orientSet != null) {
      return orientSet;
    }

    Set<PageOrientation> oset = new TreeSet<PageOrientation>();
    OrientationRequested[] or = null;
    try {
      or =
          (OrientationRequested[])
              service.getSupportedAttributeValues(OrientationRequested.class, null, null);
    } catch (Exception e) {
    }
    if (or == null || or.length == 0) {
      oset.add(defaultOrientation());
    } else {
      for (int i = 0; i < or.length; i++) {
        if (or[i] == OrientationRequested.PORTRAIT) {
          oset.add(PageOrientation.PORTRAIT);
        } else if (or[i] == OrientationRequested.REVERSE_PORTRAIT) {
          oset.add(PageOrientation.REVERSE_PORTRAIT);
        } else if (or[i] == OrientationRequested.LANDSCAPE) {
          oset.add(PageOrientation.LANDSCAPE);
        } else {
          oset.add(PageOrientation.REVERSE_LANDSCAPE);
        }
      }
    }
    orientSet = Collections.unmodifiableSet(oset);
    return orientSet;
  }
Exemplo n.º 15
0
 public Set<PrintQuality> supportedPrintQuality() {
   if (qualitySet == null) {
     Set<PrintQuality> set = new TreeSet<PrintQuality>();
     javax.print.attribute.standard.PrintQuality[] arr = null;
     try {
       arr =
           (javax.print.attribute.standard.PrintQuality[])
               service.getSupportedAttributeValues(
                   javax.print.attribute.standard.PrintQuality.class, null, null);
     } catch (Exception e) {
     }
     if (arr == null || arr.length == 0) {
       set.add(PrintQuality.NORMAL);
     } else {
       for (int i = 0; i < arr.length; i++) {
         if (arr[i] == javax.print.attribute.standard.PrintQuality.NORMAL) {
           set.add(PrintQuality.NORMAL);
         }
         if (arr[i] == javax.print.attribute.standard.PrintQuality.DRAFT) {
           set.add(PrintQuality.DRAFT);
         }
         if (arr[i] == javax.print.attribute.standard.PrintQuality.HIGH) {
           set.add(PrintQuality.HIGH);
         }
       }
     }
     qualitySet = Collections.unmodifiableSet(set);
   }
   return qualitySet;
 }
Exemplo n.º 16
0
 public PageOrientation defaultOrientation() {
   if (defOrient == null) {
     OrientationRequested orient =
         (OrientationRequested) service.getDefaultAttributeValue(OrientationRequested.class);
     defOrient = reverseMapOrientation(orient);
   }
   return defOrient;
 }
Exemplo n.º 17
0
 Chromaticity getDefaultChromaticity() {
   Chromaticity color = null;
   try {
     color = (Chromaticity) service.getDefaultAttributeValue(Chromaticity.class);
   } catch (Exception e) {
     color = Chromaticity.COLOR;
   }
   return color;
 }
Exemplo n.º 18
0
 SheetCollate getDefaultSheetCollate() {
   SheetCollate collate = null;
   try {
     collate = (SheetCollate) service.getDefaultAttributeValue(SheetCollate.class);
   } catch (Exception e) {
     collate = SheetCollate.UNCOLLATED;
   }
   return collate;
 }
Exemplo n.º 19
0
  private synchronized void refreshServices() {
    printers = getAllPrinterNames();
    if (printers == null) {
      // In Windows it is safe to assume no default if printers == null so we
      // don't get the default.
      printServices = null;
      return;
    }

    PrintService[] newServices = new PrintService[printers.length];
    PrintService defService = getDefaultPrintService();
    for (int p = 0; p < printers.length; p++) {
      if (defService != null && printers[p].equals(defService.getName())) {
        newServices[p] = defService;
      } else {
        if (printServices == null) {
          newServices[p] = new Win32PrintService(printers[p]);
        } else {
          int j;
          for (j = 0; j < printServices.length; j++) {
            if ((printServices[j] != null) && (printers[p].equals(printServices[j].getName()))) {
              newServices[p] = printServices[j];
              printServices[j] = null;
              break;
            }
          }
          if (j == printServices.length) {
            newServices[p] = new Win32PrintService(printers[p]);
          }
        }
      }
    }

    // Look for deleted services and invalidate these
    if (printServices != null) {
      for (int j = 0; j < printServices.length; j++) {
        if ((printServices[j] instanceof Win32PrintService)
            && (!printServices[j].equals(defaultPrintService))) {
          ((Win32PrintService) printServices[j]).invalidateService();
        }
      }
    }
    printServices = newServices;
  }
Exemplo n.º 20
0
  @Override
  public void print() {

    try {
      File input = new File(absoluteFilePath);
      FileInputStream fis = new FileInputStream(input);
      FileChannel fc = fis.getChannel();
      ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

      PDFFile curFile = null;
      PDFPrintPage pages = null;
      curFile = new PDFFile(bb); // Create PDF Print Page
      pages = new PDFPrintPage(curFile);
      PrinterJob pjob = PrinterJob.getPrinterJob();

      PrintService[] services = PrinterJob.lookupPrintServices();
      for (PrintService ps : services) {
        String pName = ps.getName();
        if (pName.equals(Profile.getInstance().getDefaultPrinterName())) {
          pjob.setPrintService(ps);
          logger.debug("Printing on : " + pName);
          break;
        }
      }

      pjob.setJobName(absoluteFilePath);
      Book book = new Book();
      PageFormat pformat = PrinterJob.getPrinterJob().defaultPage();
      book.append(pages, pformat, curFile.getNumPages());
      pjob.setPageable(book);

      // print
      PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

      // Print it
      pjob.print(aset);
      fis.close();
    } catch (Exception e) {
      logger.error("Cannot print pdf", e);
      Dialog.showThrowable("Erreur", "Impossible d'imprimer le fichier", e);
    }
  }
Exemplo n.º 21
0
 ServiceNotifier(PrintService service) {
   super(service.getName() + " notifier");
   this.service = service;
   listeners = new Vector();
   try {
     setPriority(Thread.NORM_PRIORITY - 1);
     setDaemon(true);
     start();
   } catch (SecurityException e) {
   }
 }
Exemplo n.º 22
0
 PrinterResolution getDefaultPrinterResolution() {
   PrinterResolution res =
       (PrinterResolution) service.getDefaultAttributeValue(PrinterResolution.class);
   /* I think it may be possible for this to be just unsupported,
    * so do I need to allow for that somehow ?
    */
   if (res == null) {
     res = new PrinterResolution(300, 300, ResolutionSyntax.DPI);
   }
   return res;
 }
Exemplo n.º 23
0
 public Paper defaultPaper() {
   if (defPaper != null) {
     return defPaper;
   }
   Media m = (Media) service.getDefaultAttributeValue(Media.class);
   if (m == null || !(m instanceof MediaSizeName)) {
     defPaper = Paper.NA_LETTER;
   } else {
     defPaper = getPaper((MediaSizeName) m);
   }
   return defPaper;
 }
Exemplo n.º 24
0
  javax.print.attribute.standard.PrintQuality getDefaultPrintQuality() {

    javax.print.attribute.standard.PrintQuality quality = null;
    try {
      quality =
          (javax.print.attribute.standard.PrintQuality)
              service.getDefaultAttributeValue(javax.print.attribute.standard.PrintQuality.class);
    } catch (Exception e) {
      quality = javax.print.attribute.standard.PrintQuality.NORMAL;
    }
    return quality;
  }
Exemplo n.º 25
0
 public int defaultCopies() {
   if (defaultCopies > 0) {
     return defaultCopies;
   }
   try {
     Copies copies = (Copies) service.getDefaultAttributeValue(Copies.class);
     defaultCopies = copies.getValue();
   } catch (Exception e) {
     defaultCopies = 1;
   }
   return defaultCopies;
 }
 /* Want the PrintService which is default print service to have
  * equality of reference with the equivalent in list of print services
  * This isn't required by the API and there's a risk doing this will
  * lead people to assume its guaranteed.
  */
 public PrintService[] getPrintServices() {
   SecurityManager security = System.getSecurityManager();
   if (security != null) {
     security.checkPrintJobAccess();
   }
   if (printServices == null) {
     PrintService defService = getDefaultPrintService();
     printers = getAllPrinterNames();
     if (printers == null) {
       return new PrintService[0];
     }
     printServices = new PrintService[printers.length];
     for (int p = 0; p < printers.length; p++) {
       if (defService != null && printers[p].equals(defService.getName())) {
         printServices[p] = defService;
       } else {
         printServices[p] = new Win32PrintService(printers[p]);
       }
     }
   }
   return printServices;
 }
Exemplo n.º 27
0
  // set it protected to make it testable
  private IPrintingService getPrintingService(final I_AD_PrinterRouting route) {
    if (LogManager.isLevelFine()) {
      log.debug("Checking route: " + route);
    }

    final I_AD_Printer printer = route.getAD_Printer();
    if (LogManager.isLevelFine()) {
      log.debug("Printer: " + printer.getPrinterName());
    }

    final PrintService systemPrintService = getSystemPrintService(printer.getPrinterName());
    if (systemPrintService == null) {
      log.info("Printer not found in system: " + printer.getPrinterName());
      return null;
    }
    if (LogManager.isLevelFine()) {
      log.debug("System Print Service: " + systemPrintService);
    }

    final String printerName = systemPrintService.getName();
    Boolean isDirectPrint = null;
    if (isDirectPrint == null && route.getIsDirectPrint() != null) {
      isDirectPrint = X_AD_PrinterRouting.ISDIRECTPRINT_Yes.equals(route.getIsDirectPrint());
      if (LogManager.isLevelFine()) {
        log.debug("IsDirectPrint: " + isDirectPrint + " (From: " + route + ")");
      }
    }
    if (isDirectPrint == null) {
      isDirectPrint = isDirectPrint(printer);
    }

    final PrintingServiceImpl printingService =
        new PrintingServiceImpl(printerName, printer.getPrinterType(), isDirectPrint);

    if (LogManager.isLevelFine()) {
      log.debug("Printing Service: " + printingService);
    }
    return printingService;
  }
 boolean matchingService(PrintService service, PrintServiceAttributeSet serviceSet) {
   if (serviceSet != null) {
     Attribute[] attrs = serviceSet.toArray();
     Attribute serviceAttr;
     for (int i = 0; i < attrs.length; i++) {
       serviceAttr = service.getAttribute(attrs[i].getCategory());
       if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
         return false;
       }
     }
   }
   return true;
 }
Exemplo n.º 29
0
  /**
   * Fix for bug ID 6255588 from Sun bug database
   *
   * @param job print job that the fix applies to
   */
  public static void initPrinterJobFields(PrinterJob job) {
    try {
      PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
      PrintService printService = PrintServiceLookup.lookupDefaultPrintService();

      String printerName = PrintConfig.getOsReceiptPrinterName();
      if (printToKitchen) {
        printerName = PrintConfig.getOsKitchenPrinterName();
      }

      for (int i = 0; i < printServices.length; i++) {
        PrintService service = printServices[i];
        if (service.getName().equals(printerName)) {
          printService = service;
          break;
        }
      }

      job.setPrintService(printService);
      printToKitchen = false;
    } catch (PrinterException e) {
    }
  }
Exemplo n.º 30
0
 public PrintSides defaultSides() {
   if (defSides != null) {
     return defSides;
   }
   Sides sides = (Sides) service.getDefaultAttributeValue(Sides.class);
   if (sides == null || sides == Sides.ONE_SIDED) {
     defSides = PrintSides.ONE_SIDED;
   } else if (sides == Sides.DUPLEX) {
     defSides = PrintSides.DUPLEX;
   } else {
     defSides = PrintSides.TUMBLE;
   }
   return defSides;
 }