import javax.print.*; class Test { public static void main (String [] args) { PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); System.out.println("Number of print services: " + printServices.length); for (PrintService printer : printServices) System.out.println("Printer: " + printer.getName()); } }
public static void main(String[] args) throws PrintException, IOException { DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE; PrintRequestAttributeSet patts = new HashPrintRequestAttributeSet(); patts.add(Sides.DUPLEX); PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, patts); if (ps.length == 0) { throw new IllegalStateException("No Printer found"); } System.out.println("Available printers: " + Arrays.asList(ps)); PrintService myService = null; for (PrintService printService : ps) { if (printService.getName().equals("Your printer name")) { myService = printService; break; } } if (myService == null) { throw new IllegalStateException("Printer not found"); } FileInputStream fis = new FileInputStream("C:/Users/John Doe/Desktop/SamplePDF.pdf"); Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null); DocPrintJob printJob = myService.createPrintJob(); printJob.print(pdfDoc, new HashPrintRequestAttributeSet()); fis.close(); }
PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); if(service != null) { DocFlavor docFlawor = DocFlavor.INPUT_STREAM.PDF; PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); DocPrintJob job = printService.createPrintJob(); Doc pdf = new SimpleDoc(new URL(url).openStream(), docFlawor, null); job.print(pdf, printRequestAttributeSet); }
byte[] bytes = bos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); // First identify the default print service on the system PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); // prepare the flvaor you are intended to print DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.PDF; // prepare the print job DocPrintJob printJob = printService.createPrintJob(); // prepare the document, with default attributes, ready to print Doc docToPrint = new SimpleDoc( bais, docFlavor, null ); // now send the doc to print job, with no attributes to print printJob.print( docToPrint, null );
printServiceAttributes.add(new PrinterName(printerName, locale)); PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, printServiceAttributes); if (printServices.length > 0) { printer = printServices[0]; Debug.logInfo("Using printer: " + printer.getName(), module); if (!printer.isDocFlavorSupported(psInFormat)) { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPrinterNotSupportDocFlavorFormat", UtilMisc.toMap("psInFormat", psInFormat, "printerName", printer.getName()), locale)); printer = PrintServiceLookup.lookupDefaultPrintService(); if (printer != null) { Debug.logInfo("No printer name supplied, using default printer: " + printer.getName(), module); PrintRequestAttributeSet praset = new HashPrintRequestAttributeSet(); List<Object> printRequestAttributes = UtilGenerics.checkList(serviceContext.remove("printRequestAttributes")); if (UtilValidate.isNotEmpty(printRequestAttributes)) { DocPrintJob job = printer.createPrintJob(); job.print(myDoc, praset); } catch (PrintException | IOException | TemplateException | GeneralException | SAXException | ParserConfigurationException e) { Debug.logError(e, "Error rendering [" + contentType + "]: " + e.toString(), module);
import java.awt.print.*; import javax.print.*; import javax.print.attribute.*; import java.text.*; import javax.print.attribute.standard.*; public class ShowPrinters { public ShowPrinters() { } public static void main(String[] args) { DocFlavor myFormat = DocFlavor.SERVICE_FORMATTED.PRINTABLE; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); PrintService[] services =PrintServiceLookup.lookupPrintServices(myFormat, aset); System.out.println("The following printers are available"); for (int i=0;i<services.length;i++) { System.out.println(" service name: "+services[i].getName()); } } }
PrintService service = PrintServiceLookup.lookupDefaultPrintService(); DocPrintJob job = service.createPrintJob(); Doc doc = new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null); job.addPrintJobListener(pjDone); job.print(doc, null);
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); PrintServiceAttribute attr = services[i].getAttribute(PrinterName.class); sPrinterName = ((PrinterName) attr).getValue(); if (sPrinterName.toLowerCase().indexOf(printerName) >= 0) { throw new ZebraPrintNotFoundException("Zebra printer not found : " + printerName); DocPrintJob job = psZebra.createPrintJob(); job.print(doc, null);
PrintService ps = PrinterJob.getPrinterJob().getPrintService(); DocFlavor[] myFlavors = ps.getSupportedDocFlavors(); ps.addPrintServiceAttributeListener(PrintTest.this); DocPrintJob docJob = ps.createPrintJob(); docJob.addPrintJobAttributeListener(PrintTest.this, null); docJob.addPrintJobListener(PrintTest.this); try { docJob.print(PrintTest.this,null); PrinterStateReasons psr = pje.getPrintJob().getPrintService().getAttribute(PrinterStateReasons.class); if (psr != null) { Set<PrinterStateReason> errors = psr.printerStateReasonSet(Severity.REPORT); PrinterStateReasons psr = pje.getPrintJob().getPrintService().getAttribute(PrinterStateReasons.class); if (psr != null) { Set<PrinterStateReason> errors = psr.printerStateReasonSet(Severity.REPORT);
String pName = ps.getName(); if(pName.equalsIgnoreCase("PrinterName")){ pjob.setPrintService(ps); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
for (int i = 0; !printerFound && i < printService.length; i++) if (printService[i].getName().contains(printerName))
PrintService pservice = ... // acquire print service of your printer DocPrintJob job = pservice.createPrintJob(); String commands = ""; commands += "\u001B\u0045\u000A"; // plain commands += "Hello "; commands += "\u001B\u0045\u000D"; // bold commands += "ESCP!"; DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; Doc doc = new SimpleDoc(commands.getBytes(), flavor, null); job.print(doc, null);
public void print(PrinterJob printerJob) throws PrinterException { HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); Media[] res = (Media[]) printerJob.getPrintService().getSupportedAttributeValues(Media.class, null, null); for (int i = 0; i < res.length; i++) { System.out.println("res[" + i + "]=" + res[i]); System.out.println(res[i].toString()); if (res[i].toString().equals(printerMedia)) { attributes.add(res[i]); } } printerJob.print(attributes); PageFormat pageFormat = printerJob.getPageFormat(attributes); System.out.println("[after opening]"); printPageFormat(pageFormat); pageFormat.setOrientation(PageFormat.PORTRAIT); printerJob.setPrintable(new TestSequenceInstanceLabel.MyPrintable(this), pageFormat); try { printerJob.print(); } catch (PrinterException ex) { ex.printStackTrace(); } }
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);
public TableModel produce( final DataRow parameters, final DataFactoryContext dataFactoryContext ) throws ReportDataFactoryException { final PrintService[] services = PrintServiceLookup.lookupPrintServices( DocFlavor.SERVICE_FORMATTED.PAGEABLE, null ); final TypedTableModel tt = new TypedTableModel(); tt.addColumn( "Printer ID", String.class ); tt.addColumn( "Printer Name", String.class ); for ( int i = 0; i < services.length; i++ ) { final PrintService service = services[i]; final PrinterName displayName = service.getAttribute( PrinterName.class ); if ( displayName != null ) { tt.addRow( new Object[] { service.getName(), displayName.getValue() } ); } else { tt.addRow( new Object[] { service.getName(), service.getName() } ); } } return tt; } }
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); PrintService printService = null; for(PrintService ps : printServices){ if(ps.getName().equals(printer)){ printService = ps; break; jrExporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, printService); jrExporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printService.getAttributes()); jrExporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE); jrExporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE);
String printerName = combobox.getSelectedItem().toString(); for (PrintService p: PrintServiceLookup.lookupPrintServices(null, null)) { if (p.getName().equals(printerName)) { doc.print(p.getAttributes()); break; } }
public ClarionPrinter() { CWin.getInstance(); services=PrinterJob.lookupPrintServices(); defaultService=PrintServiceLookup.lookupDefaultPrintService(); if (getJob()!=null) { setProperty(Propprint.DEVICE,getJob().getPrintService().getName()); } }
PrintService service = PrintServiceLookup.lookupDefaultPrintService(); int count = 0; for (DocFlavor docFlavor : service.getSupportedDocFlavors()) { if (docFlavor.toString().contains("pdf")) { count++; } } if (count == 0) { System.err.println("PDF not supported by printer: " + service.getName()); System.exit(1); } else { System.out.println("PDF is supported by printer: " + service.getName()); }
public class MediaTest { public static void main(String[] args) { PrintService[] printServices = PrintServiceLookup.lookupPrintServices( DocFlavor.SERVICE_FORMATTED.PRINTABLE, null); for (PrintService printService : printServices) { System.out.println(printService.getName()); Media[] supportedMedia = (Media[]) printService.getSupportedAttributeValues( Media.class, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null); for (Media media : supportedMedia) { if (media instanceof MediaSizeName) { handleMediaSizeName((MediaSizeName) media); } } } } private static void handleMediaSizeName(MediaSizeName mediaSizeName) { StringBuilder builder = new StringBuilder(" " + mediaSizeName.toString()); MediaSize mediaSize = MediaSize.getMediaSizeForName(mediaSizeName); if (mediaSize != null) { float[] size = mediaSize.getSize(MediaSize.MM); builder.append(" - ").append(size[0]).append("x").append(size[1]).append("mm"); } System.out.println(builder.toString()); } }