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); } } }
/** * Takes a printer name and find the associated PrintService. If no match can be made it randomly * picks the first printer listed from the call to lookupPrintServices. * * @param printerName * @return PrintService referenced by the printerName */ public PrintService getPrinterInternal(final String printerName, final String lastPrinterName) { // The parameter value was not provided, and we are allowed to create // user interface forms PrintService[] services = PrinterJob.lookupPrintServices(); for (PrintService element : services) { if (element.getName().equals(printerName)) { return element; } } if (feedbackAllowed()) { // If it's not valid then lets find one and end this current run. ArrayList values = new ArrayList(); for (PrintService element : services) { String value = element.getName(); values.add(value); } createFeedbackParameter( StandardSettings.PRINTER_NAME, Messages.getInstance().getString("PrintComponent.USER_PRINTER_NAME"), "", lastPrinterName, values, null, "select"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ promptNeeded(); return null; } return services[0]; }
public static PrintService getDefaultPrinter() { // The parameter value was not provided, and we are allowed to create // user interface forms PrintService[] services = PrinterJob.lookupPrintServices(); if ((services == null) || (services.length == 0)) { return null; } return services[0]; }
public PrintServiceVO[] printerList() { List<PrintServiceVO> observableList = new ArrayList<PrintServiceVO>(); PrintService printService[] = PrinterJob.lookupPrintServices(); for (int i = 0; i < printService.length; i++) { PrintServiceVO printServiceVO = new PrintServiceVO(); printServiceVO.setPrintService(printService[i]); observableList.add(printServiceVO); } PrintServiceVO[] array = observableList.toArray(new PrintServiceVO[observableList.size()]); return array; }
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(); } }
/** * Takes a printer name and find the associated PrintService. If no match can be made it randomly * picks the first printer listed from the call to lookupPrintServices. * * @param printerName * @return PrintService referenced by the printerName */ public static PrintService getPrinter(final String printerName) { // The parameter value was not provided, and we are allowed to create // user interface forms PrintService[] services = PrinterJob.lookupPrintServices(); for (PrintService element : services) { if (element.getName().equals(printerName)) { return element; } } return services[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); } }
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(); } }