private boolean printData(PrintService printService , String printText) { try { SimpleDoc doc; doc = new SimpleDoc(printText.getBytes(), javax.print.DocFlavor.BYTE_ARRAY.AUTOSENSE, null); DocPrintJob job = printService.createPrintJob(); job.print(doc, new HashPrintRequestAttributeSet()); System.out.println("Job sent to printer succesfully"); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
Doc myDoc = new SimpleDoc(psStream, psInFormat, null); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
Doc myDoc = new SimpleDoc(psStream, psInFormat, null); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
/** * Print a range of pages from the document as specified by #setupPrintService. * * @throws PrintException if a default printer could not be found or some * other printing related error. */ public void print() throws PrintException { // make sure we have a service, if not we assign the default printer if (printService == null && services != null && services.length > 0 && services[0] != null) { printService = services[0]; } if (printService != null) { // calculate total pages being printed calculateTotalPagesToPrint(); printService.createPrintJob().print( new SimpleDoc(this, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null), printRequestAttributeSet); } else { logger.fine("No print could be found to print to."); } }
throw new NullPointerException("Input Stream is null: file not found?"); Doc doc = new SimpleDoc(is, flavor, null);
@Override public boolean print(byte[] content, JsonNode configuration, User user) { try { DocFlavor flavor = new DocFlavor("application/vnd.cups-raster", "[B"); for (PrintService service : PrintServiceLookup.lookupPrintServices(flavor, new HashPrintRequestAttributeSet())) { DocPrintJob job = service.createPrintJob(); Doc doc = new SimpleDoc(content, flavor, null); job.print(doc, null); return true; } log.error("No printer found"); return false; } catch (PrintException e) { log.error("print", e); } return false; } },
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; String content = makeZplLabel(); Doc doc = new SimpleDoc(content.getBytes(), flavor, docAttr);
public CancelablePrintJob cancelablePrint() throws PrintException { // make sure we have a service, if not we assign the default printer if (printService == null && services != null && services.length > 0 && services[0] != null) { printService = services[0]; } if (printService != null) { // calculate total pages being printed calculateTotalPagesToPrint(); DocPrintJob printerJob = printService.createPrintJob(); printerJob.print( new SimpleDoc(this, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null), printRequestAttributeSet); return (CancelablePrintJob) printerJob; } else { return null; } }
Doc doc = new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
import java.io.FileInputStream; import javax.print.Doc; import javax.print.DocFlavor; import javax.print.DocPrintJob; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.SimpleDoc; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.standard.Copies; public class Main { static public void main(String args[]) throws Exception { PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); pras.add(new Copies(1)); PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras); if (pss.length == 0) throw new RuntimeException("No printer services available."); PrintService ps = pss[0]; System.out.println("Printing to " + ps); DocPrintJob job = ps.createPrintJob(); FileInputStream fin = new FileInputStream("YOurImageFileName.PNG"); Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null); job.print(doc, pras); fin.close(); } }
Doc doc = new SimpleDoc (fis, flavor, null); pj.print (doc, aset);
PrintService ps=PrintServiceLookup.lookupDefaultPrintService(); System.out.println(ps.getName()); ps.createPrintJob().print(new SimpleDoc(new PrintDev(), flavor, null), null);
public void print(PrintJobWatcher printJobWatcher) throws PrintException { // make sure we have a service, if not we assign the default printer if (printService == null && services != null && services.length > 0 && services[0] != null) { printService = services[0]; } if (printService != null) { // calculate total pages being printed calculateTotalPagesToPrint(); DocPrintJob printerJob = printService.createPrintJob(); printJobWatcher.setPrintJob(printerJob); printerJob.print( new SimpleDoc(this, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null), printRequestAttributeSet); printJobWatcher.waitForDone(); } else { logger.fine("No print could be found to print to."); } }
Doc doc = new SimpleDoc(by, flavor, null); job.print(doc, null);
Doc doc = new SimpleDoc(data, flavor, null);
Doc doc = new SimpleDoc(data, flavor, null);
public void print() throws IOException, PrintException { DocFlavor inputFlavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8; // Lookup a print factory to convert from desired input to output. StreamPrintServiceFactory[] psfactories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories( inputFlavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType()); if (psfactories.length == 0) { System.err.println("Ack! No StreamPrintFactory found for this job!"); } StreamPrintService printService = psfactories[0].getPrintService(new FileOutputStream("demo.ps")); PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); attrs.add(OrientationRequested.LANDSCAPE); attrs.add(MediaSizeName.NA_LETTER); attrs.add(new Copies(1)); attrs.add(new JobName(INPUT_FILE_NAME, null)); InputStream is = getClass().getResourceAsStream(INPUT_FILE_NAME); if (is == null) { throw new NullPointerException( "Input Stream is null: file not found?"); } Doc doc = new SimpleDoc(is, inputFlavor, null); DocPrintJob printJob = printService.createPrintJob(); printJob.print(doc, attrs); } }
private void printHelloDocument(String value) { PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); DocFlavor flavor = DocFlavor.READER.TEXT_PLAIN; Doc doc = new SimpleDoc(new StringReader(value), flavor, null); PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset); PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); if (services.length == 0) { if (defaultService == null) { new Notification("No printer found", WARNING_MESSAGE).show(getPage()); } else { DocPrintJob job = defaultService.createPrintJob(); printDocument(doc, aset, job); } } else { SwingUtilities.invokeLater(() -> { PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset); if (service != null) { DocPrintJob job = service.createPrintJob(); printDocument(doc, aset, job); } }); } }
if (chosenService != null) { DocPrintJob job = chosenService.createPrintJob(); Doc myDoc = new SimpleDoc(masterImage, flavor, null);//attributeSet); try { job.print(myDoc, attributeSet);
aset.add(AWTUtil.getPrintableArea(paper, margin)); Doc doc = new SimpleDoc(printable, flavor, null); DocPrintJob job = service.createPrintJob(); job.print(doc, aset);