title: PrinterJob - selecting a printer || DocPrintJob formatting the page
body: <p>I am indifferent, but just need something to work ASAP. </p>
  
  <p>The PrinterJob use works great, but the printer has to be the default printer in Windows to work. If it isn't, then it won't print. In this case, anyone know how to select a specific printer?</p>
  
  <p>The DocPrintJob allows me to use a specific printer, but the formatting is all off. It has about a 1" margin on the left and then cuts off the long 44 char string. This is going to a receipt printer.</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Font;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.geom.Line2D;
  import java.awt.print.PageFormat;
  import java.awt.print.Paper;
  import java.awt.print.Printable;
  import java.awt.print.PrinterException;
  import java.awt.print.PrinterJob;
  import java.util.ArrayList;
  
  import javax.print.DocFlavor;
  import javax.print.DocPrintJob;
  import javax.print.PrintException;
  import javax.print.PrintService;
  import javax.print.PrintServiceLookup;
  import javax.print.SimpleDoc;
  
  public class PrinterExample {
  String receiptPrinter = "Star TSP100 Cutter (TSP143)";
  
  public PrinterExample(ArrayList&lt;String&gt; text) throws PrintException, PrinterException {
  
      // this works perfectly, but the printer
      // has to be the default printer
      PrinterJob job2 = PrinterJob.getPrinterJob();
      PageFormat pf = job2.defaultPage();
      double margin = 1.0;
      Paper paper = new Paper();
      paper.setSize(216.0, (double) (paper.getHeight() + text.size() * 10.0));
      paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
      pf.setPaper(paper);
      pf.setOrientation(PageFormat.PORTRAIT);
      job2.setPrintable(new ThePrintable(text), pf);
      job2.print();
  
      // this doesn't take into account the
      // size of the sheet of paper and chops off
      // the text
  
      DocPrintJob job = getPrinterJob(receiptPrinter);
      DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
      SimpleDoc doc = new SimpleDoc(new ThePrintable(text), flavor, null);
      job.print(doc, null);
  
  }
  
  public static void printIdentificationPages() {
      DocPrintJob job = null;
      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());
          job = printer.createPrintJob();
          DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
          ArrayList&lt;String&gt; printArray = new ArrayList&lt;&gt;();
          printArray.add("This is a test page to");
          printArray.add("help you identify the");
          printArray.add("printer you want to use.");
          printArray.add("");
          printArray.add(printer.getName());
  
          SimpleDoc doc = new SimpleDoc(new ThePrintable(printArray), flavor, null);
          try {
              job.print(doc, null);
          } catch (PrintException e) {
              e.printStackTrace();
          }
      }
  }
  
  private static DocPrintJob getPrinterJob(String printerToUse) {
      DocPrintJob job = null;
      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());
          if (printer.getName().equals(printerToUse)) {
              job = printer.createPrintJob();
          }
      }
      return job;
  }
  
  public static void main(String... args) {
      ArrayList&lt;String&gt; printArray = new ArrayList&lt;&gt;();
      printArray.add("Hello");
      printArray.add("World!");
      printArray.add("");
      printArray.add("12345678901234567890123456789012345678901234");
      printArray.add("");
  
      try {
          new PrinterExample(printArray);
      } catch (PrintException | PrinterException e) {
          e.printStackTrace();
      }
  
      // Printer.printIdentificationPages();
  }
  
  }
  
  class ThePrintable implements Printable {
  
  ArrayList&lt;String&gt; text = new ArrayList&lt;&gt;();
  
  public ThePrintable(ArrayList&lt;String&gt; text) {
      this.text = text;
  }
  
  public int print(Graphics g, PageFormat pf, int page) {
      if (page &gt; 0) {
          return NO_SUCH_PAGE;
      }
      int i;
      Graphics2D g2d = (Graphics2D) g;
      Line2D.Double line = new Line2D.Double();
      g2d.setColor(Color.black);
      g2d.translate(pf.getImageableX(), pf.getImageableY());
      int y = 15;
  
      g.setFont(new Font("Consolas", Font.BOLD, 8));
      for (String s : text) {
          g2d.drawString(s, 1, y);
          y += 15;
      }
  
      return PAGE_EXISTS;
  }
  }
  </code></pre>