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(); } // } }
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(); } } }
// {{{ getPageFormat() method public static PageFormat getPageFormat() { // convert from PrintRequestAttributeSet to the pageFormat PrinterJob prnJob = getPrintJob(" "); 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(MediaSize.INCH) * 72), (ms.getY(MediaSize.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; } // }}}
public void setPrintArea( double printAreaTop, double printAreaLeft, double printAreaWidth, double printAreaHeight) { super.setImageableArea( printAreaTop * k, printAreaLeft * k, printAreaWidth * k, printAreaHeight * k); }