Exemple #1
0
    @Override
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
      if (page > 0) {
        /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
      }

      /* User (0,0) is typically outside the imageable area, so we must
       * translate by the X and Y values in the PageFormat to avoid clipping
       */
      Graphics2D g2d = (Graphics2D) g;
      Paper paper = new Paper();
      paper.setImageableArea(0, 0, 560, 540);
      paper.setSize(560, 540);
      // PageFormat format = new PageFormat();
      pf.setOrientation(PageFormat.LANDSCAPE);
      Dimension dimension = this.getSize();
      double compWidth = dimension.width;
      double compHeight = dimension.height;
      double pageWidth = pf.getImageableWidth();
      double pageHeight = pf.getImageableHeight();
      double scaleWidth = pageWidth / compWidth;
      double scaleHeight = pageHeight / compHeight;
      pf.setPaper(paper);
      g2d.scale(scaleWidth, scaleHeight);
      g2d.translate(pf.getImageableX(), pf.getImageableY());
      print(g2d);
      /* Now we perform our rendering */

      /* tell the caller that this page is part of the printed document */
      return PAGE_EXISTS;
    }
  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();
    }
  }
  public synchronized void printThisInvoice(JTable tbl) {
    this.tblprint = tbl;
    // this.totrecords = totrec;

    PrinterJob pj = PrinterJob.getPrinterJob();

    PageFormat pf = new PageFormat();

    Paper paper = new Paper();

    paper.setSize(WIDTH, HEIGHT);
    paper.setImageableArea(0, 0, WIDTH, HEIGHT);
    pf.setPaper(paper);
    pj.setPrintable(this, pf);

    // if (pj.printDialog()) {
    try {
      System.out.println("Starting........");
      pj.print();
      System.out.println("Done!......");
    } catch (Exception e) {
      System.out.println("Error: Print Error: " + e);
      e.printStackTrace();
    }
    //  }

  }
Exemple #4
0
 /** @param args */
 public static void main(String[] args) throws Throwable {
   PrinterJob job = PrinterJob.getPrinterJob();
   PageFormat format = new PageFormat();
   format.setOrientation(format.PORTRAIT);
   Paper paper = new Paper();
   paper.setSize(3 * 72, 5 * 72);
   paper.setImageableArea(0, 0, 1200 * 3, 1200 * 5);
   job.setPrintable(new TestPrintable(), format);
   job.print();
 }
  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();
    }
  }
Exemple #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);
    }
  }
 public void setPaperSize(PaperSize s) {
   paper.setSize(s.PaperWidth, s.PaperHeight);
   paper.setImageableArea(s.ImageableX, s.ImageableY, s.ImageableWidth, s.ImageableHeight);
   if (s == A4) {
     for (PrintMech pm : Mechs) {
       pm.setA4();
     }
   } else if (s == Letter) {
     for (PrintMech pm : Mechs) {
       pm.setLetter();
     }
   }
 }
Exemple #8
0
  public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = new Paper();
    paper.setSize(237, 414); // Large Address Dimension
    paper.setImageableArea(0, 0, 0, 0);
    pageFormat.setPaper(paper);
    pageFormat.setOrientation(PageFormat.LANDSCAPE);

    printJob.setPrintable(this, pageFormat);
    if (printJob.printDialog())
      try {
        printJob.print();
      } catch (PrinterException pe) {
        System.out.println("Error printing: " + pe);
      }
  }
  public void print() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pageFormat = job.defaultPage();
    Paper paper = pageFormat.getPaper();

    paper.setImageableArea(
        0.0D, 0.0D, pageFormat.getPaper().getWidth(), pageFormat.getPaper().getHeight());
    pageFormat.setPaper(paper);

    job.setJobName(documentTitle);
    job.setPrintable(this, pageFormat);
    if (job.printDialog())
      try {
        job.print();
      } catch (PrinterException ex) {
        JOptionPane.showMessageDialog(null, "Failed to print" + ex);
      }
  }
  public static void main(String[] args) throws Exception {
    PDFJob job = new PDFJob(new FileOutputStream("test.pdf"));
    PageFormat pf = new PageFormat();
    pf.setOrientation(PageFormat.PORTRAIT);
    Paper p = new Paper();
    p.setSize(210, 297); // A4
    pf.setPaper(p);

    BufferedImage img = ImageIO.read(new File("earth.jpg"));

    int w = 200;

    for (int i = 0; i < 10; i++) {
      Graphics g = job.getGraphics();
      g.drawImage(img, 0, 0, w, w, null);
      g.dispose();
    }

    job.end();
  }
  // {{{ getPageFormat() method
  public static PageFormat getPageFormat() {
    // convert from PrintRequestAttributeSet to the pageFormat
    PrinterJob prnJob = getPrintJob(" "); // $NON-NLS-1$
    PageFormat pf = prnJob.defaultPage();
    Paper pap = pf.getPaper();

    MediaSizeName media = (MediaSizeName) format.get(Media.class);
    MediaSize ms = MediaSize.getMediaSizeForName(media);

    MediaPrintableArea mediaarea = (MediaPrintableArea) format.get(MediaPrintableArea.class);
    if (mediaarea != null) {
      pap.setImageableArea(
          (mediaarea.getX(MediaPrintableArea.INCH) * 72),
          (mediaarea.getY(MediaPrintableArea.INCH) * 72),
          (mediaarea.getWidth(MediaPrintableArea.INCH) * 72),
          (mediaarea.getHeight(MediaPrintableArea.INCH) * 72));
    }
    if (ms != null) {
      pap.setSize((ms.getX(Size2DSyntax.INCH) * 72), (ms.getY(Size2DSyntax.INCH) * 72));
    }
    pf.setPaper(pap);

    OrientationRequested orientation =
        (OrientationRequested) format.get(OrientationRequested.class);
    if (orientation != null) {
      if (orientation.getValue() == OrientationRequested.LANDSCAPE.getValue()) {
        pf.setOrientation(PageFormat.LANDSCAPE);
      } else if (orientation.getValue() == OrientationRequested.REVERSE_LANDSCAPE.getValue()) {
        pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
      } else if (orientation.getValue() == OrientationRequested.PORTRAIT.getValue()) {
        pf.setOrientation(PageFormat.PORTRAIT);
      } else if (orientation.getValue() == OrientationRequested.REVERSE_PORTRAIT.getValue()) {
        // doesnt exist??
        // pf.setOrientation(PageFormat.REVERSE_PORTRAIT);
        // then just do the next best thing
        pf.setOrientation(PageFormat.PORTRAIT);
      }
    }
    return pf;
  } // }}}
 protected static String dump(Paper paper) {
   StringBuilder sb = new StringBuilder(64);
   sb.append(paper.getWidth())
       .append("x")
       .append(paper.getHeight())
       .append("/")
       .append(paper.getImageableX())
       .append("x")
       .append(paper.getImageableY())
       .append(" - ")
       .append(paper.getImageableWidth())
       .append("x")
       .append(paper.getImageableHeight());
   return sb.toString();
 }
 // Print image
 public void printImage() {
   PrinterJob printJob = PrinterJob.getPrinterJob();
   PageFormat pf = printJob.defaultPage();
   pf.setOrientation(PageFormat.LANDSCAPE);
   ////////////////////////////////////////
   Paper paper = new Paper();
   paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
   pf.setPaper(paper);
   printJob.setPrintable(this, pf);
   printJob.setJobName("Custom Waiver");
   System.out.println(paper.getWidth() + " " + paper.getHeight());
   try {
     printJob.print();
     // justPrint();
   } catch (Exception PrintException) {
     PrintException.printStackTrace();
   }
 }
Exemple #14
0
 public void setPrintArea(
     double printAreaTop, double printAreaLeft, double printAreaWidth, double printAreaHeight) {
   super.setImageableArea(
       printAreaTop * k, printAreaLeft * k, printAreaWidth * k, printAreaHeight * k);
 }
    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();
      }
    }
  @Override
  public boolean performFinish() {

    // create the document
    Rectangle suggestedPageSize = getITextPageSize(page1.getPageSize());
    Rectangle pageSize = rotatePageIfNecessary(suggestedPageSize); // rotate if we need landscape

    Document document = new Document(pageSize);

    try {

      // Basic setup of the Document, and get instance of the iText Graphics2D
      //   to pass along to uDig's standard "printing" code.
      String outputFile =
          page1.getDestinationDir()
              + System.getProperty("file.separator")
              + //$NON-NLS-1$
              page1.getOutputFile();
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
      document.open();
      Graphics2D graphics = null;
      Template template = getTemplate();

      int i = 0;
      int numPages = 1;
      do {

        // sets the active page
        template.setActivePage(i);

        PdfContentByte cb = writer.getDirectContent();

        Page page = makePage(pageSize, document, template);

        graphics = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight());

        // instantiate a PrinterEngine (pass in the Page instance)
        PrintingEngine engine = new PrintingEngine(page);

        // make page format
        PageFormat pageFormat = new PageFormat();
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        java.awt.print.Paper awtPaper = new java.awt.print.Paper();
        awtPaper.setSize(pageSize.getWidth() * 3, pageSize.getHeight() * 3);
        awtPaper.setImageableArea(0, 0, pageSize.getWidth(), pageSize.getHeight());
        pageFormat.setPaper(awtPaper);

        // run PrinterEngine's print function
        engine.print(graphics, pageFormat, 0);

        graphics.dispose();
        document.newPage();
        if (i == 0) {
          numPages = template.getNumPages();
        }
        i++;

      } while (i < numPages);

      // cleanup
      document.close();
      writer.close();
    } catch (DocumentException e) {
      e.printStackTrace();
      return false;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    } catch (PrinterException e) {
      e.printStackTrace();
    }
    return true;
  }
  private boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog)
      throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0
        || firstPageIndex > lastPageIndex
        || lastPageIndex >= jasperPrint.getPages().size()) {
      throw new JRException(
          "Invalid page index range : "
              + firstPageIndex
              + " - "
              + lastPageIndex
              + " of "
              + jasperPrint.getPages().size());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName("JasperReports - " + jasperPrint.getName());

    switch (jasperPrint.getOrientation()) {
      case JRReport.ORIENTATION_LANDSCAPE:
        {
          pageFormat.setOrientation(PageFormat.LANDSCAPE);
          paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
          paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
          break;
        }
      case JRReport.ORIENTATION_PORTRAIT:
      default:
        {
          pageFormat.setOrientation(PageFormat.PORTRAIT);
          paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
          paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
      if (withPrintDialog) {
        if (printJob.printDialog()) {
          printJob.print();
        } else {
          isOK = false;
        }
      } else {
        printJob.print();
      }
    } catch (Exception ex) {
      throw new JRException("Error printing report.", ex);
    }

    return isOK;
  }
Exemple #18
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();
  }
 /**
  * 设置打印样式
  *
  * @param width
  * @param height
  * @return
  */
 private Paper setPapeSize(double width, double height) {
   Paper p = new Paper();
   p.setSize(width, height); // 纸张大小
   p.setImageableArea(0, 0, width, height);
   return p;
 }