printerjob.setPrintService(printService); // Try setting the printer you want } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Error: No printer named '" + printerName + "', using default printer.");
printJob.setPrintService(printService[i]); printerFound = true;
PrinterJob printJob = PrinterJob.getPrinterJob(); PrintService service = PrintServiceLookup.lookupDefaultPrintService(); printJob.setPrintService(service); document.silentPrint(printJob);
PrinterJob printJob = PrinterJob.getPrinterJob(); PrintService service = PrintServiceLookup.lookupDefaultPrintService(); printJob.setPrintService(service); document.silentPrint(printJob);
JasperPrint print = JasperFillManager.fillReport(this.class.getResource("/classpath/yourReport.jasper").getPath(), new HashMap(), new yourReportDataSource()); PrinterJob job = PrinterJob.getPrinterJob(); /* Create an array of PrintServices */ PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); int selectedService = 0; /* Scan found services to see if anyone suits our needs */ for(int i = 0; i < services.length;i++){ if(services[i].getName().toUpperCase().contains("Your printer's name")){ /*If the service is named as what we are querying we select it */ selectedService = i; } } job.setPrintService(services[selectedService]);
/** Restore the PrintJob's PrintService to what it was originally. */ private void restoreServiceReference() { try { thePrintJob.setPrintService(delegate); } catch (PrinterException e) { AuditLog.logError(LOGGER, e, "Restore Print Service"); //$NON-NLS-1$ } }
/** * Fix for bug ID 6255588 from Sun bug database * @param job print job that the fix applies to */ public static void initPrinterJobFields(PrinterJob job) { try { job.setPrintService(job.getPrintService()); } catch (PrinterException e) { } }
PrinterJob pj = PrinterJob.getPrinterJob(); PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); System.out.println("Number of printers configured: " + printServices.length); for (PrintService printer : printServices) { System.out.println("Printer: " + printer.getName()); if (printer.getName().equals("***MYPRINTER***")) { try { pj.setPrintService(printer); } catch (PrinterException ex) { } } }
public static boolean printToPostScript(File file, Printable printable) { try { if(!file.exists()) { file.createNewFile(); } PrinterJob job = PrinterJob.getPrinterJob(); String mime = "application/postscript"; FileOutputStream out = new FileOutputStream(file.getAbsolutePath()); StreamPrintService printer; StreamPrintServiceFactory[] factories = PrinterJob.lookupStreamPrintServices(mime); if(factories.length > 0) { printer = factories[0].getPrintService(out); job.setPrintService(printer); job.setPrintable(printable); job.print(); } else { System.out.println("No suitable print services"); return false; } } catch(Exception e) { e.printStackTrace(); return false; } return true; }
String printerNameDesired = "My Printer"; PrintService[] service = PrinterJob.lookupPrintServices(); // list of printers int count = service.length; for (int i = 0; i < count; i++) { if (service[i].getName().equalsIgnoreCase(printerNameDesired)) { docPrintJob = service[i].createPrintJob(); i = count; } } PrinterJob pjob = PrinterJob.getPrinterJob(); pjob.setPrintService(docPrintJob.getPrintService()); pjob.setJobName("job"); pjob.print();
PDDocument doc = PDDocument.load(new FileInputStream(System.getProperty("java.io.tmpdir") + "\\pdf.pdf")); //read pdf file. String printerNameDesired = "VENDOR THERMAL PRINTER"; javax.print.PrintService[] service = PrinterJob.lookupPrintServices(); DocPrintJob docPrintJob = null; int count = service.length; for (int i = 0; i < count; i++) { if (service[i].getName().equalsIgnoreCase(printerNameDesired)) { docPrintJob = service[i].createPrintJob(); i = count; } } PrinterJob pjob = PrinterJob.getPrinterJob(); pjob.setPrintService(docPrintJob.getPrintService()); pjob.setJobName("job"); doc.silentPrint(pjob);
/** * Utility for showing print dialog for the current printService. If no * print service is assigned the first print service is used to create * the print dialog. */ public void showPrintSetupDialog() { PrinterJob pj = PrinterJob.getPrinterJob(); if (printService == null && services != null && services.length > 0 && services[0] != null) { printService = services[0]; } try { pj.setPrintService(printService); // Step 2: Pass the settings to a page dialog and print dialog. pj.pageDialog(printRequestAttributeSet); } catch (Throwable e) { logger.log(Level.FINE, "Error creating page setup dialog.", e); } }
final PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintService(ps); final PageFormat pf = printJob.defaultPage(); System.out.println("Printer Page width=" + pf.getWidth() + " height=" + pf.getHeight());
public static void main(String args[]) throws Exception { PDDocument document = PDDocument.load(new File("C:/temp/example.pdf")); PrintService myPrintService = findPrintService("My Windows printer Name"); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); job.setPrintService(myPrintService); job.print(); } private static PrintService findPrintService(String printerName) { PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); for (PrintService printService : printServices) { if (printService.getName().trim().equals(printerName)) { return printService; } } return null; }
private boolean print(PrintService printService, InputStream inputStream, PrintRequestAttributeSet attributes) throws PrintException { try { PDDocument pdf = PDDocument.load(inputStream); PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintService(printService); job.setPageable(new PDFPageable(pdf)); job.print(attributes); pdf.close(); } catch (PrinterException e) { logger.error("Error when printing PDF file using the printer {}", printService.getName(), e); throw new PrintException("Printer exception", e); } catch (IOException e) { logger.error("Error when loading PDF from input stream", e); throw new PrintException("Input exception", e); } return true; }
HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet(); attr.add(new MediaPrintableArea(0f, 0f, w/72f, h/72f, MediaPrintableArea.INCH)); PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintService(ps); job.setPrintable(this); job.setJobName(jobName); job.print(attr); j.setVisible(false); j.dispose();
// get default service PrintService service = PrintServiceLookup.lookupDefaultPrintService(); // this is your old awt job, which you can use to get default pageFormat PrinterJob job = PrinterJob.getPrinterJob(); // set your printer service to old awt so that you can get the default paper job.setPrintService(service); // get the default page format from the printer you selected PageFormat pageFormat = job.getPageFormat(null); // do some paper stuff etc. here // get your doc DocPrintJob printJob = service.createPrintJob(); Book book = new Book(); // Print interface implements printable book.append(new PrintInterface(), pageFormat); // make your attr here PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(PrintQuality.HIGH); // Create your doc for pageable SimpleDoc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); /// and voila, print it printJob.print(doc, aset);
public void print(String imageFileName) throws IOException, PrinterException { final PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); final BufferedImage image = ImageIO.read(new File(imageFileName)); final PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setJobName("MyApp: " + imageFileName); printJob.setPrintService(printService); printJob.setPrintable(new Printable() { @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex == 0) { final Paper paper = pageFormat.getPaper(); paper.setImageableArea(0.0, 0.0, pageFormat.getPaper().getWidth(), pageFormat.getPaper().getHeight()); pageFormat.setPaper(paper); graphics.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY()); graphics.drawImage(image, 0, 0, (int) pageFormat.getPaper().getWidth(), (int) pageFormat.getPaper().getHeight(), null); return PAGE_EXISTS; } else { return NO_SUCH_PAGE; } } }); printJob.print(); }
public void print() { PrinterJob printerJob = PrinterJob.getPrinterJob(); PrintService[] printService = PrinterJob.lookupPrintServices(); for (int i = 0; i < printService.length; i++) { System.out.println(printService[i].getName()); if (printService[i].getName().compareTo(printerName) == 0) { try { printerJob.setPrintService(printService[i]); setDoubleBuffered(false); setSize(getPreferredSize()); addNotify(); validate(); print(printerJob); } catch (PrinterException e) { e.printStackTrace(); } } } }
PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintService(printer); PageFormat pageFormat = job.defaultPage(); Paper paper = pageFormat.getPaper(); Book pBook = new Book(); PDDocument document = PDDocument.load(new URL(fileUrl).openStream()); // Needs to be a stream now PDPageTree allPages = document.getPages(); // no getDocumentCatalog for (Object pdPage : allPages) { PDPage page = (PDPage) pdPage; double width = page.getMediaBox().getWidth(); // getMediaBox instead of find double height = page.getMediaBox().getHeight(); // getMediaBox instead of find double margin = 0.0d; paper.setSize(width, height); paper.setImageableArea(margin, margin, width - (margin * 2), height - (margin * 2)); pageFormat.setOrientation(PageFormat.PORTRAIT); pageFormat.setPaper(paper); PageFormat validatePage = job.validatePage(pageFormat); pBook.append(new PDFPrintable(document), validatePage, document.getNumberOfPages()); // Completely different } job.setPageable(pBook); // Moved this out of the loop, just cause it only needs to be called once job.setJobName(jobName); job.print();