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?