/*

  public void printData() {

  getJMenuBar().repaint();

  try {

  PrinterJob prnJob = PrinterJob.getPrinterJob();

  prnJob.setPrintable(m_panel);

  if (!prnJob.printDialog())

  return;

  setCursor( Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

  prnJob.print();

  setCursor( Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

  JOptionPane.showMessageDialog(this, "Printing completed successfully", "JPEGEditor2",

  JOptionPane.INFORMATION_MESSAGE);

  }

  catch (PrinterException e) {

  e.printStackTrace();

  System.err.println("Printing error: "+e.toString());

  }

  }

  */
  public void printData(Image im) {

    Printer p = new Printer(im);

    try {

      java.awt.print.PrinterJob prnJob = java.awt.print.PrinterJob.getPrinterJob();

      prnJob.setPrintable(p);

      if (!prnJob.printDialog()) {
        return;
      }

      setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));

      prnJob.print();

      setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));

      javax.swing.JOptionPane.showMessageDialog(
          this,
          "Printing completed successfully",
          "JPEGEditor2",
          javax.swing.JOptionPane.INFORMATION_MESSAGE);

    } catch (java.awt.print.PrinterException e) {

      e.printStackTrace();

      System.err.println("Printing_error:_" + e.toString());
    }
  }
  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 #3
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;
   }
 }
  public static void print(JTextPane textPane) {

    PrinterJob job = PrinterJob.getPrinterJob();
    PrintableHandler printable = new PrintableHandler(textPane);
    job.setPrintable(printable);
    boolean print = job.printDialog();

    if (print) {
      try {
        job.print();
      } catch (PrinterException ex) {
        ex.printStackTrace();
      }
    }
  }
    @Override
    public void stopRenderer() throws IOException {
      super.stopRenderer();

      try {
        printerJob.print();
      } catch (PrinterException e) {
        e.printStackTrace();
        throw new IOException(
            Messages.getInstance()
                .getString(
                    "PrintComponent.ERROR_0003_UNABLE_TO_PRINT",
                    e.getClass().getName(),
                    e.getMessage())); // $NON-NLS-1$
      }
    }
 /*
  * Calls up printing prompt for gnomon.
  */
 public void printGnomon() {
   PrinterJob pjob = PrinterJob.getPrinterJob();
   PageFormat preformat = pjob.defaultPage();
   PageFormat postformat = pjob.pageDialog(preformat);
   // If user does not hit cancel then print.
   if (preformat != postformat) {
     // Set print component
     pjob.setPrintable(
         new SundialPrint(frame), postformat); // this is what's needed to integrate it
     if (pjob.printDialog()) {
       try {
         pjob.print();
       } catch (PrinterException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
   }
 }
Example #7
0
  public void actionPerformed(ActionEvent ae) {

    if (ae.getSource() == b) {

      PrinterJob job = PrinterJob.getPrinterJob();
      job.setPrintable(this);
      boolean ok = job.printDialog();

      if (ok) {
        try {
          job.print();
        } catch (PrinterException pe) {
          System.out.println(pe);
          pe.printStackTrace();
        }
      }
    }

    if (ae.getSource() == back) {
      new remindersub();
      f.dispose();
    }
  }
  @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;
  }
    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();
      }
    }