title: Fix text stretching when printing to a receipt printer with Java
body: <p>I am printing to some Epson receipt printers by implementing the Java Printable and placing my code into the print method. To draw the text to the printer I use Graphics2D.drawString. I am also drawing a rect to the printer to see how to compares to the text size when printing to other printers. When printing to the receipt printer the text on the paper is about double the width of printing to a laser printer or the XPS writer virtual print. Is this a problem with the way Java draws text to the Graphics2D object? I have the newest version of Java installed of 6 update 20.</p>
  
  <p>Any Ideas of what to look into would be helpful.</p>
  
  <p>Thanks.</p>
  
  <p>Here the code I am using. With this example I am seeing the letter 'c' on the right edge of the rect when sending it to a XPS writer and if I print it to my receipt printer the 6 is on the right edge of the rect and you can tell the text is much wider then it should be. The rect seems to be the correct size.</p>
  
  <p>I have tried changing the page and margin sizes but it does not seem to fix my text problem. I got these paper sizes and margins from how Microsoft Word is auto detecting the printer. Word prints the text correctly to the receipt printer.</p>
  
  <pre><code>public static void main(String[] args) {
     PageFormat format = new PageFormat();
     Paper paper = new Paper();
  
     double paperWidth = 3.25;
     double paperHeight = 11.69;
     double leftMargin = 0.19;
     double rightMargin = 0.25;
     double topMargin = 0;
     double bottomMargin = 0.01;
  
     paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
     paper.setImageableArea(leftMargin * 72.0, topMargin * 72.0,
          (paperWidth - leftMargin - rightMargin) * 72.0,
          (paperHeight - topMargin - bottomMargin) * 72.0);
  
     format.setPaper(paper);
  
     PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
     aset.add(OrientationRequested.PORTRAIT);
  
     PrinterJob printerJob = PrinterJob.getPrinterJob();
     Printable printable = new ReceiptPrintTest();
     format = printerJob.validatePage(format);
     printerJob.setPrintable(printable, format);
     try {
        printerJob.print(aset);
     }
     catch (Exception e) {
         e.printStackTrace();
     }
  }
  
  
  
  public class ReceiptPrintTest implements Printable {
  
      public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
  
        if (pageIndex &lt; 0 || pageIndex &gt;= 1) {
              return Printable.NO_SUCH_PAGE;
          }
  
          Graphics2D g2d = (Graphics2D) graphics;
          g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
  
          Font font = new Font("Arial",Font.PLAIN, 14);
  
          g2d.setFont(font);
          g2d.drawString("1234567890abcdefg", 50, 70);
  
          g2d.drawRect(50, 0, 100, 50);
  
          return Printable.PAGE_EXISTS;
      }
  </code></pre>