OK, finally, I found a good solution for my billing task, and it works correctly for me.
This class provides a print service.
public class PrinterService { public PrintService getCheckPrintService(String printerName) { PrintService ps = null; DocFlavor doc_flavor = DocFlavor.STRING.TEXT_PLAIN; PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet(); attr_set.add(new Copies(1)); attr_set.add(Sides.ONE_SIDED); PrintService[] service = PrintServiceLookup.lookupPrintServices(doc_flavor, attr_set); for (int i = 0; i < service.length; i++) { System.out.println(service[i].getName()); if (service[i].getName().equals(printerName)) { ps = service[i]; } } return ps; } }
This class demonstrates the task of printing an invoice,
public class HelloWorldPrinter implements Printable { @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D) graphics; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
source share