Exemplo n.º 1
1
        public Print2DPrinterJob() {

                /* Construct the print request specification.
                * The print data is a Printable object.
                * the request additonally specifies a job name, 2 copies, and
                * landscape orientation of the media.
                */
                PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                aset.add(OrientationRequested.LANDSCAPE);
                aset.add(new Copies(2));
                aset.add(new JobName("My job", null));

                /* Create a print job */
                PrinterJob pj = PrinterJob.getPrinterJob();       
                pj.setPrintable(this);
                /* locate a print service that can handle the request */
                PrintService[] services =
                        PrinterJob.lookupPrintServices();

                if (services.length > 0) {
                        System.out.println("selected printer " + services[0].getName());
                        try {
                                pj.setPrintService(services[0]);
                                pj.pageDialog(aset);
                                if(pj.printDialog(aset)) {
                                        pj.print(aset);
                                }
                        } catch (PrinterException pe) { 
                                System.err.println(pe);
                        }
                }
        }
  public void print(HashPrintRequestAttributeSet aset) {

    try {
      for (int pos = 0; pos < smallCraftList.size(); pos++) {
        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setPrintService(masterPrintJob.getPrintService());

        aset.add(PrintQuality.HIGH);

        PageFormat pageFormat = new PageFormat();
        pageFormat = pj.getPageFormat(null);

        Paper p = pageFormat.getPaper();
        p.setImageableArea(0, 0, p.getWidth(), p.getHeight());
        pageFormat.setPaper(p);

        pj.setPrintable(this, pageFormat);
        smallCraft = smallCraftList.get(pos);
        pj.setJobName(smallCraft.getChassis() + " " + smallCraft.getModel());

        try {
          pj.print(aset);
        } catch (Exception ex) {
          ex.printStackTrace();
        }
        System.gc();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
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);
   }
 }
Exemplo n.º 5
0
  private void defaultPrintPage(InvoicePagePrint pageprintdocumnet) {
    PrintService[] service = PrinterJob.lookupPrintServices(); // list
    // of
    // printers
    int count = service.length;
    PrintService printSvc = null;
    for (int i = 0; i < count; i++) {
      System.out.println("Service name: " + service[i].getName());
      if (service[i].getName().indexOf("PDFcamp") != -1) {
        printSvc = service[i];
        i = count;
      }
    }

    //  TSP650
    PageFormat format = new PageFormat();
    Paper paper = new Paper();

    double paperWidth = 3.25;
    double paperHeight = 11.69;
    double leftMargin = 0.19;
    double rightMargin = 0.25;
    double topMargin = 0;
    double bottomMargin = 0.01;

    paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
    paper.setImageableArea(
        leftMargin * 72.0,
        topMargin * 72.0,
        (paperWidth - leftMargin - rightMargin) * 72.0,
        (paperHeight - topMargin - bottomMargin) * 72.0);

    format.setPaper(paper);

    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(OrientationRequested.PORTRAIT);

    PrinterJob printerJob = PrinterJob.getPrinterJob();
    try {
      printerJob.setPrintService(printSvc);
    } catch (PrinterException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    // Printable printable = new ReceiptPrintTest();
    format = printerJob.validatePage(format);
    // printerJob.setPrintable(printable, format);
    printerJob.setPrintable(pageprintdocumnet, format);
    try {
      printerJob.print(aset);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 6
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.º 7
0
 private PrintableBase doPrintAutoImpl(
     Container cont,
     PrinterJob job,
     StreamPrintService ps,
     int pOrientation,
     Paper paper,
     int offscrnImageType,
     int dpi,
     int numSamples,
     int tileWidth,
     int tileHeight,
     boolean resizeWithinPrintTest) {
   try {
     PageFormat pageFormat = job.defaultPage();
     if (null != paper) {
       /**
        * Paper paper = new Paper(); paper.setSize(500,500); // Large Address Dimension
        * paper.setImageableArea(20, 20, 450, 420);
        */
       pageFormat.setPaper(paper);
     }
     pageFormat.setOrientation(pOrientation); // PageFormat.LANDSCAPE or PageFormat.PORTRAIT
     job.setPrintService(ps);
     final PrintableBase printable;
     if (0 < offscrnImageType) {
       printable =
           new OffscreenPrintable(
               job,
               cont,
               dpi,
               numSamples,
               tileWidth,
               tileHeight,
               offscrnImageType,
               getPrintFilename(
                   offscrnImageType,
                   dpi,
                   numSamples,
                   tileWidth,
                   tileHeight,
                   "png",
                   resizeWithinPrintTest));
     } else {
       printable = new OnscreenPrintable(job, cont, dpi, numSamples, tileWidth, tileHeight);
     }
     printable.job.setPrintable(printable, pageFormat);
     doPrintImpl(printable, resizeWithinPrintTest);
     return printable;
   } catch (PrinterException pe) {
     pe.printStackTrace();
     return null;
   }
 }
Exemplo n.º 8
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.º 9
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.º 10
0
  public static void startPrinting(
      IApplication application,
      Pageable pageable,
      PrinterJob a_printerJob,
      String a_preferredPrinterName,
      boolean showPrinterSelectDialog,
      boolean avoidDialogs) {
    RepaintManager currentManager =
        RepaintManager.currentManager(application.getPrintingRendererParent().getParent());
    boolean isDoubleBufferingEnabled = false;
    try {
      if (currentManager != null) {
        isDoubleBufferingEnabled = currentManager.isDoubleBufferingEnabled();
      }

      if (a_printerJob != null) {
        // for plugin printing
        a_printerJob.setPageable(pageable);
        a_printerJob.setJobName("Servoy Print"); // $NON-NLS-1$
        if (showPrinterSelectDialog) {
          if (!a_printerJob.printDialog()) {
            return;
          }
          SwingHelper.dispatchEvents(100); // hide dialog
        }
        if (currentManager != null) {
          currentManager.setDoubleBufferingEnabled(false);
        }
        a_printerJob.print();
      } else {
        // by default we use old system for mac, new is not always working
        boolean useSystemPrintDialog =
            Utils.getAsBoolean(
                application
                    .getSettings()
                    .getProperty(
                        "useSystemPrintDialog",
                        "" + Utils.isAppleMacOS())); // $NON-NLS-1$//$NON-NLS-2$
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        if (capablePrintServices == null) {
          capablePrintServices = PrintServiceLookup.lookupPrintServices(flavor, pras);
        }

        PrintService service = null;
        if (capablePrintServices == null || capablePrintServices.length == 0) {
          if (avoidDialogs) {
            Debug.warn("Cannot find capable print services. Print aborted.");
            return;
          } else {
            JOptionPane.showConfirmDialog(
                ((ISmartClientApplication) application).getMainApplicationFrame(),
                application.getI18NMessage("servoy.print.msg.noPrintersFound"),
                application.getI18NMessage(
                    "servoy.print.printing.title"), //$NON-NLS-1$ //$NON-NLS-2$
                JOptionPane.OK_OPTION,
                JOptionPane.ERROR_MESSAGE);
            capablePrintServices = new PrintService[0];
            showPrinterSelectDialog = true; // must show select printer and if none found show this
            useSystemPrintDialog =
                true; // we leave to system to show no printers are found, important for apple mac
          }
        } else {
          service = capablePrintServices[0]; // default select
        }

        PrintService systemDefaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
        if (systemDefaultPrinter != null) {
          // check if system default printer is in capable list
          for (PrintService ps : capablePrintServices) {
            if (ps.getName().equalsIgnoreCase(systemDefaultPrinter.getName())) {
              service = systemDefaultPrinter;
              break;
            }
          }
        }

        boolean didFindPrinter = true; // want custom preferred printer
        if (a_preferredPrinterName != null) {
          didFindPrinter = false;
          for (PrintService ps : capablePrintServices) {
            if (ps.getName().equalsIgnoreCase(a_preferredPrinterName)) {
              didFindPrinter = true;
              service = ps;
              break;
            }
          }
        }

        if (!didFindPrinter) {
          if (avoidDialogs) {
            Debug.warn(
                "Cannot find capable printer for preferred form printer name '"
                    + a_preferredPrinterName
                    + "'. Trying to use default/any capable printer.");
          } else {
            showPrinterSelectDialog = true; // did not found the prefered , do show
          }
        }

        if (!useSystemPrintDialog) {
          if (showPrinterSelectDialog) {
            JFrame frame = ((ISmartClientApplication) application).getMainApplicationFrame();
            GraphicsConfiguration gc = frame.getGraphicsConfiguration();
            Point loc = frame.getLocation();
            service =
                ServiceUI.printDialog(
                    gc, loc.x + 50, loc.y + 50, capablePrintServices, service, flavor, pras);
          }
          if (service != null) {
            if (currentManager != null) {
              currentManager.setDoubleBufferingEnabled(false);
            }
            DocPrintJob job = service.createPrintJob();
            DocAttributeSet das = new HashDocAttributeSet();
            Doc doc = new SimpleDoc(pageable, flavor, das);
            if (job != null) {
              job.print(doc, pras);
            } else {
              // for example if the print service cancels (e.g. print to pdf and then user cancel
              // when choosing save location)
              application.reportWarning(
                  application.getI18NMessage(
                      "servoy.print.error.cannotPrintDocument")); //$NON-NLS-1$
            }
          }
        } else {
          a_printerJob = PrinterJob.getPrinterJob();
          a_printerJob.setPageable(pageable);
          a_printerJob.setJobName("Servoy Print"); // $NON-NLS-1$
          if (service != null) {
            a_printerJob.setPrintService(service);
          }
          if (showPrinterSelectDialog) {
            if (!a_printerJob.printDialog()) {
              return;
            }
            SwingHelper.dispatchEvents(100); // hide dialog
          }
          if (currentManager != null) {
            currentManager.setDoubleBufferingEnabled(false);
          }
          a_printerJob.print();
        }
      }
    } catch (Exception ex) {
      application.reportError(
          application.getI18NMessage("servoy.print.error.cannotPrintDocument"), ex); // $NON-NLS-1$
    } finally {
      if (currentManager != null) {
        currentManager.setDoubleBufferingEnabled(isDoubleBufferingEnabled);
      }
    }
  }
Exemplo n.º 11
0
  private void printDocument(InvoicePagePrint pageprintdocumnet) {
    PrintService[] service = PrinterJob.lookupPrintServices(); // list
    // of
    // printers
    int count = service.length;
    PrintService printSvc = null;
    for (int i = 0; i < count; i++) {
      System.out.println("Service name: " + service[i].getName());
      if (service[i].getName().indexOf("PDFcamp") != -1) {
        printSvc = service[i];
        i = count;
      }
    }

    // --- Create a new PrinterJob object and set predefined print service.
    try {

      PrinterJob printJob = PrinterJob.getPrinterJob();
      printJob.setPrintService(printSvc);

      PageFormat pf = printJob.defaultPage();
      // Paper paper = pf.getPaper();
      // paper.setSize(8.5 * 72, 11 * 72);
      // paper.setSize(79, 78);

      // paper.setImageableArea(0.5 * 72, 0.0 * 72, 7.5 * 72, 11.5 * 72);
      // paper.setImageableArea(0.5 , 0.0 , 79, 78);
      // pf.setPaper(paper);

      // --- Create a new book to add pages to
      Book book = new Book();

      // --- Add the cover page using the default page format for this
      // print
      // job
      // book.append(pageprintdocumnet, printJob.defaultPage());
      book.append(pageprintdocumnet, pf);

      // Paper pg = new Paper();
      // pg.setSize(1000, 900);
      // book.append(pageprintdocumnet, printJob..defaultPage();

      // --- Add the document page using a landscape page format
      /*
       * PageFormat documentPageFormat = new PageFormat();
       * documentPageFormat.setOrientation(PageFormat.LANDSCAPE); book.append(new Document(),
       * documentPageFormat);
       */
      // --- Add a third page using the same painter

      // --- Tell the printJob to use the book as the pageable object
      printJob.setPageable(book);

      // --- Show the print dialog box. If the user click the
      // --- print button we then proceed to print else we cancel
      // --- the process.
      // if (printJob.printDialog()) {

      printJob.print();
    } catch (Exception PrintException) {
      PrintException.printStackTrace();
    }
  }