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();
    }
  }
Example #2
0
  /** Convenience method */
  public void print() throws PrinterException {
    //
    // Now, request the transcoder to actually perform the
    // printing job.
    //
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    PageFormat pageFormat = printerJob.defaultPage();

    //
    // Set the page parameters from the hints
    //
    Paper paper = pageFormat.getPaper();

    Float pageWidth = (Float) hints.get(KEY_PAGE_WIDTH);
    Float pageHeight = (Float) hints.get(KEY_PAGE_HEIGHT);
    if (pageWidth != null) {
      paper.setSize(pageWidth.floatValue(), paper.getHeight());
    }
    if (pageHeight != null) {
      paper.setSize(paper.getWidth(), pageHeight.floatValue());
    }

    float x = 0, y = 0;
    float width = (float) paper.getWidth();
    float height = (float) paper.getHeight();

    Float leftMargin = (Float) hints.get(KEY_MARGIN_LEFT);
    Float topMargin = (Float) hints.get(KEY_MARGIN_TOP);
    Float rightMargin = (Float) hints.get(KEY_MARGIN_RIGHT);
    Float bottomMargin = (Float) hints.get(KEY_MARGIN_BOTTOM);

    if (leftMargin != null) {
      x = leftMargin.floatValue();
      width -= leftMargin.floatValue();
    }
    if (topMargin != null) {
      y = topMargin.floatValue();
      height -= topMargin.floatValue();
    }
    if (rightMargin != null) {
      width -= rightMargin.floatValue();
    }
    if (bottomMargin != null) {
      height -= bottomMargin.floatValue();
    }

    paper.setImageableArea(x, y, width, height);

    String pageOrientation = (String) hints.get(KEY_PAGE_ORIENTATION);
    if (VALUE_PAGE_ORIENTATION_PORTRAIT.equalsIgnoreCase(pageOrientation)) {
      pageFormat.setOrientation(PageFormat.PORTRAIT);
    } else if (VALUE_PAGE_ORIENTATION_LANDSCAPE.equalsIgnoreCase(pageOrientation)) {
      pageFormat.setOrientation(PageFormat.LANDSCAPE);
    } else if (VALUE_PAGE_ORIENTATION_REVERSE_LANDSCAPE.equalsIgnoreCase(pageOrientation)) {
      pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
    }

    pageFormat.setPaper(paper);
    pageFormat = printerJob.validatePage(pageFormat);

    //
    // If required, pop up a dialog to adjust the page format
    //
    Boolean showPageFormat = (Boolean) hints.get(KEY_SHOW_PAGE_DIALOG);
    if ((showPageFormat != null) && (showPageFormat.booleanValue())) {
      PageFormat tmpPageFormat = printerJob.pageDialog(pageFormat);
      if (tmpPageFormat == pageFormat) {
        // Dialog was cancelled, meaning that the print process should
        // be stopped.
        return;
      }

      pageFormat = tmpPageFormat;
    }

    // Set printable before showing printer dialog so
    // it can update the pageFormat if it wishes...
    printerJob.setPrintable(this, pageFormat);

    //
    // If required, pop up a dialog to select the printer
    //
    Boolean showPrinterDialog;
    showPrinterDialog = (Boolean) hints.get(KEY_SHOW_PRINTER_DIALOG);
    if (showPrinterDialog != null && showPrinterDialog.booleanValue()) {
      if (!printerJob.printDialog()) {
        // Dialog was cancelled, meaning that the print process
        // should be stopped.
        return;
      }
    }

    // Print now
    printerJob.print();
  }
    public void printPage(String size) {
      double width = 0.0;
      double height = 0.0;
      double margin = 0.0;
      PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
      try {
        PrinterJob pj = PrinterJob.getPrinterJob();
        if (pj.printDialog()) {
          PageFormat pf = pj.defaultPage();
          Paper paper = pf.getPaper();
          if (size.equals("3r")) {
            width = 5d * 72d;
            height = 3.5d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.JIS_B7);
          } else if (size.equals("4r")) {
            width = 6d * 72d;
            height = 4d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.JAPANESE_POSTCARD);
          } else if (size.equals("5r")) {
            width = 7d * 72d;
            height = 5d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.NA_5X7);
          } else if (size.equals("6r")) {
            width = 8d * 72d;
            height = 6d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.JAPANESE_DOUBLE_POSTCARD);
          } else if (size.equals("8r")) {
            width = 10d * 72d;
            height = 8d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.NA_8X10);
          }
          paper.setSize(width, height);
          paper.setImageableArea(margin, margin, width - (margin * 2), height - (margin * 2));
          System.out.println("Before- " + dump(paper));
          pf.setOrientation(PageFormat.LANDSCAPE);
          pf.setPaper(paper);
          System.out.println("After- " + dump(paper));
          System.out.println("After- " + dump(pf));
          dump(pf);
          PageFormat validatePage = pj.validatePage(pf);
          System.out.println("Valid- " + dump(validatePage));

          Book pBook = new Book();
          pBook.append(this, pf);
          pj.setPageable(pBook);

          aset.add(OrientationRequested.LANDSCAPE);

          try {
            pj.setPrintable(this);
            pj.print(aset);
          } catch (PrinterException ex) {
            ex.printStackTrace();
          }
        }

      } catch (Exception exp) {
        exp.printStackTrace();
      }
    }