0

This is billing application (like super market billing). I used the following code to print some thing in the default external printer (Posiflex)

I am working with spring-boot, maven, Java.

package com.spring.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;

public class Printer implements Printable {
public static void main(String[] args) {

  Printer example1 = new Printer();
  System.exit(0);
}


 private final double INCH = 25;

 public Printer() {

 PrinterJob printJob = PrinterJob.getPrinterJob();

 printJob.setPrintable(this);

  try {
    printJob.print();
  } catch (Exception PrintException) {
    PrintException.printStackTrace();
  }

 }


public int print(Graphics g, PageFormat pageFormat, int page) {

 if (page == 0) {  


Graphics2D g2d = (Graphics2D) g;
g.setFont(new Font("TimesRoman", Font.PLAIN, 10)); 
FontMetrics fontMetrics = g.getFontMetrics();
  g2d.setColor(Color.black);
  g2d.translate(pageFormat.getImageableWidth(), pageFormat
        .getImageableHeight());

  g.drawString("XYZ Textiles", 0, 10);
  g.drawString("no 123, abc street, state", 0, 30);
  g.drawString("country, zip xxxxxx", 0, 50);
  g.drawString("sales@xyztex.com", 0, 70);

  return (PAGE_EXISTS);
} else
  return (NO_SUCH_PAGE);
}

} 

It prints the text successfully in the printer. But it started printing from the middle of the printer paper roll.

I gave x-co-ordinate as 0 even though it started from the middle

Actual

------------------------------------
|                                  |
|                                  |
|              XYZ Textiles        |
|              no 123, abc street, |
|              country, zip xxxxxx |
|              sales@xyztex.com    |
|                                  |
------------------------------------

Expected

------------------------------------
|                                  |
|                                  |
|           XYZ Textiles           |
|        no 123, abc street        |
|       country, zip xxxxxx        |
|        sales@xyztex.com          |
|                                  |
------------------------------------

How can I customize this to print as expected?

Gnik
  • 7,120
  • 20
  • 79
  • 129
  • Have a look at what the `imagableWidth/Height/x/y` properties are returning – MadProgrammer Jun 22 '17 at 05:00
  • You may need to adjust the margins of the paper, for [example](https://stackoverflow.com/questions/13558152/how-can-i-print-a-custom-paper-size-cheques-8-x-4/13558335#13558335), [example](https://stackoverflow.com/questions/11803741/printing-in-java-to-label-printer/11805237#11805237) and [example](https://stackoverflow.com/questions/15197872/the-print-output-to-real-printer-is-different-than-the-one-with-virtual-printer/15199029#15199029) – MadProgrammer Jun 22 '17 at 05:01

0 Answers0