title: ESC/P Set Absolute Horizontal Print Position
body: <p>I am having trouble with setting the horizontal print position in an Epson LX-300 II dot matrix printer. The command to set the horizontal print position does not work sometimes.</p>
  
  <p>I need this to align the printable data to the appropriate column in the pre-printed paper form.</p>
  
  <p>Given the following code:</p>
  
  <pre><code>        // row 1
          escp.setAbsoluteHorizontalPosition(1);
          escp.print("Equipment");
          escp.setAbsoluteHorizontalPosition(10);
          escp.print("Serial");
          escp.setAbsoluteHorizontalPosition(13);
          escp.print("Remarks");
  
          // row 2
          escp.lineFeed();
          escp.setAbsoluteHorizontalPosition(1);
          escp.print("Equipment");
          escp.setAbsoluteHorizontalPosition(10);
          escp.print("Serial");
          escp.setAbsoluteHorizontalPosition(13);
          escp.print("Remarks");
  </code></pre>
  
  <p>This is the expected output:</p>
  
  <pre><code>Equipment                  Serial     Remarks
  Equipment                  Serial     Remarks
  </code></pre>
  
  <p>However, this is the actual printed output:</p>
  
  <pre><code>EquipmentSerial                        Remarks
  EquipmentSerial                        Remarks
  </code></pre>
  
  <p>I cannot figure out why "Serial" is not printed on the correct position.</p>
  
  <p>To try to figure this out, I made a test program which prints 'x' 1cm apart in a single line.</p>
  
  <p>Here is a sample code from the test program:</p>
  
  <pre><code>    ESCPrinter escp = new ESCPrinter(sharedPrinterName, false);
      if((escp.initialize()) == false) {
          return;
      }
      escp.select10CPI();
      escp.set8LPI();
      escp.setCharacterSet(ESCPrinter.USA);                
      for(int x = 1; x &lt; 15; x++) {
          escp.setAbsoluteHorizontalPosition(x);
          escp.print("x");
      }
      escp.formFeed();
      escp.close();
  </code></pre>
  
  <p>Expected output: </p>
  
  <pre><code>x    x    x    x    x    x    x    x    x    x    x    x    x    x
  </code></pre>
  
  <p>Actual output: </p>
  
  <pre><code>x    x    x    x    xxxxxx                    x    x    x    x
  </code></pre>
  
  <p>This is the <a href="http://files.support.epson.com/pdf/general/escp2ref.pdf" rel="nofollow">Epson ESC/P Reference Manual</a>, and
  this is the <a href="http://code.google.com/p/escprinter/source/browse/trunk/net/drayah/matrixprinter/ESCPrinter.java?r=2" rel="nofollow">third-party code</a> I am using to send printer commands, which I have slightly modified to implement the set8LPI() method.</p>
  
  <p>In the reference manual, the command to set the absolute horizontal print position is in C-31.</p>
  
  <p>I have tried to figure this out myself for several days now but I am no closer to finding the solution.</p>
  
  <h2>Additional Info</h2>
  
  <p>I made another test program that prints test data from positions 0.0cm to 19.8cm, with increments of 0.2cm and line feeds in between each.</p>
  
  <p>The test data I have printed is also the absolute horizontal position I have specified (for easier identification).</p>
  
  <p>It would be impractical to show you the exact printed output, so I will just describe them as best as I can.</p>
  
  <p>Setting the horizontal position to 0.0 until 5.4 seems to work, with the output looking like this:</p>
  
  <pre><code>0.0
   0.2
    0.3
     0.4
     .
     .
     .
     --------&gt;  5.4
  </code></pre>
  
  <p>After that, setting the absolute horizontal position to 5.6cm until 10.8cm is ignored.
  The printer just prints on the left-most part of the paper.</p>
  
  <p>The "set absolute horizontal position" command is working again for 11.0cm until 16.2cm.
  Ignored again for 16.4cm until 19.8cm.</p>
  
  <p>As I understand the ESC/P reference manual I have linked above, the printer ignores this command when the specified position is beyond the right margin. Nothing else is specified.</p>
  
  <p>Obviously, my values are all within the margins.</p>
  
  <p><strike>So, could this be a hardware problem with the printer?</strike></p>
  
  <p>--> Not a hardware problem. I got same results using a different printer.</p>
  
  <h2>Update</h2>
  
  <p>Given the command to set absolute horizontal position:
  ESC $ nl nh</p>
  
  <p>I have made another test program where I directly input the nh and nl parameters.</p>
  
  <p>I have realized that this command is ignored whenever nl value exceeds 127.</p>
  
  <p>I find this to be surprising since the reference manual indicates that max value for nl is 255.</p>
  
  <p>This finding is consistent with the first test program where I input the horizontal position in centimeters. When I convert centimeters into the corresponding nh and nl values for the commands that are ignored, the resulting nl values are greater than 127.</p>