Well, my problem is that i have to print some text in a dot matrix printer with a 8.5in width x 6.5in height page.
At first, I change the paper size of the printer in the server properties from windows and it fixed the problem but now i need to print some piece of text at the beginning of the paper but i cant beause the program use some kind of margin.
I Used the following code:
public Printer(Receipt receipt )
{
this.receipt = receipt;
Thread t = new Thread(new Runnable(){
@Override
public void run() {
try {
PrinterJob printer = PrinterJob.getPrinterJob();
printer.setPrintService(getPrintService("EPSON LQ-590 ESC/P 2"));
PageFormat format = printer.defaultPage();
Paper p = format.getPaper();
p.setImageableArea(0, 0, p.getWidth(), p.getHeight()); // Set Margin to 0.
p.setSize(p.getWidth(), p.getHeight());
System.out.println("Width: " + p.getWidth() + " Height: " + p.getHeight()); //Printing in console the width and height before printing.!!
format.setPaper(p);
printer.setPrintable(Printer.this, format);
format.setPaper(p);
printer.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}});
t.start();
}
public int print(Graphics g, PageFormat format, int pageIndex)
throws PrinterException {
if(pageIndex > 0) return NO_SUCH_PAGE;
/* This is commented because I was testing the printer
Graphics2D g2d = (Graphics2D)g;
byte diasVencimiento = 120;
if(recibo.getClase() == 'C') diasVencimiento = 60;
DecimalFormat d;
d = new DecimalFormat("#,###.00");
Calendar c = Calendar.getInstance();
c.setTime(recibo.getFecha());
String cedula = Herramientas.getCedulaFormat(recibo.getCedula());
g2d.translate(15,3);
// Left Side
g2d.setFont(new Font("Times New Roman", Font.PLAIN, 10));
g2d.drawString(new ThreadSafeSimpleDateFormat("hh:mm:ss").format(recibo.getFecha()), 210, 107);
g2d.drawString(new ThreadSafeSimpleDateFormat("dd/MM/yyyy").format(recibo.getFecha()), 72, 122);//Fecha
g2d.drawString(recibo.getNombre().toUpperCase() + " " + recibo.getApellido().toUpperCase(), 72, 134);//Nombre Y Apellido
g2d.drawString(cedula, 72, 146);//Cedula
g2d.drawString((recibo.getCant1() == 0) ? " " : recibo.getCant1() + " " + recibo.getArticulo1().toUpperCase(), 50, 172);//Articulo1
g2d.drawString((recibo.getCant2() == 0) ? " " : recibo.getCant2() + " " + recibo.getArticulo2().toUpperCase(), 50, 184);//Articulo2
g2d.drawString((recibo.getCant3() == 0) ? " " : recibo.getCant3() + " " + recibo.getArticulo3().toUpperCase(), 50, 196);//Articulo3
g2d.drawString((recibo.getCant4() == 0) ? " " : recibo.getCant4() + " " + recibo.getArticulo4().toUpperCase(), 50, 208);//Articulo4
g2d.drawString(c.get(Calendar.DAY_OF_MONTH) + "", 15, 262);
g2d.drawString(c.get(Calendar.MONTH) + "", 120, 262);
g2d.drawString(c.get(Calendar.YEAR) + "", 232, 262);
g2d.drawString(d.format(recibo.getValor()), 192, 369);//Total Pagado
g2d.setFont(new Font("Times New Roman", Font.PLAIN, 12));
g2d.drawString("Total Pagado--------> ", 59, 232);//Total Pagado label
g2d.drawString(d.format(recibo.getValor()), 212, 232);//PRECIO Total PAGADO
g2d.setFont(new Font("Times New Roman", Font.PLAIN, 17));
g2d.drawString(recibo.getNumero() + "", 72, 390);//PRECIO Total PAGADO
// RIGHT SIDE
g2d.setFont(new Font("Times New Roman", Font.PLAIN, 10));
g2d.drawString(new ThreadSafeSimpleDateFormat("hh:mm:ss").format(recibo.getFecha()), 510, 107);
g2d.drawString(new ThreadSafeSimpleDateFormat("dd/MM/yyyy").format(recibo.getFecha()), 372, 122);//Fecha
g2d.drawString(recibo.getNombre().toUpperCase() + " " + recibo.getApellido().toUpperCase(), 372, 134);//Nombre Y Apellido
g2d.drawString(cedula, 372, 146);//Cedula
g2d.drawString((recibo.getCant1() == 0) ? " " : recibo.getCant1() + " " + recibo.getArticulo1().toUpperCase(), 350, 172);//Articulo1
g2d.drawString((recibo.getCant2() == 0) ? " " : recibo.getCant2() + " " + recibo.getArticulo2().toUpperCase(), 350, 184);//Articulo2
g2d.drawString((recibo.getCant3() == 0) ? " " : recibo.getCant3() + " " + recibo.getArticulo3().toUpperCase(), 350, 196);//Articulo3
g2d.drawString((recibo.getCant4() == 0) ? " " : recibo.getCant4() + " " + recibo.getArticulo4().toUpperCase(), 350, 208);//Articulo4
g2d.drawString(c.get(Calendar.DAY_OF_MONTH) + "", 310, 262);
g2d.drawString(c.get(Calendar.MONTH) + "", 420, 262);
g2d.drawString(c.get(Calendar.YEAR) + "", 532, 262);
g2d.drawString(d.format(recibo.getValor()), 492, 369);//Total Pagado
g2d.setFont(new Font("Times New Roman", Font.PLAIN, 12));
g2d.drawString("Total Pagado--------> ", 359, 232);//Total Pagado label
g2d.drawString(d.format(recibo.getValor()), 512, 232);//Precio Total Pagado
c.add(Calendar.DAY_OF_MONTH, diasVencimiento);
g2d.drawString("Fecha de Vencimiento: " + new ThreadSafeSimpleDateFormat("dd/MMMM/yy").format(c.getTime()), 359, 244);
g2d.setFont(new Font("Times New Roman", Font.PLAIN, 17));
g2d.drawString(recibo.getNumero() + "", 375, 390);
*/
System.out.println("Width2: " + format.getWidth() + " Height: " + format.getHeight()); Here Im Printing the width and height of the Paper when Printing.
//g.drawLine(0, 85, 900, 85);
return PAGE_EXISTS;
}
And This is the output:
Width: 655.2 Height: 464.4 --> Before Printing
Width2: 419.5275590551181 Height: 595.275590551181 -->When printing
Width2: 419.5275590551181 Height: 595.275590551181 --> This ppears twice, but it is printed once so I dont know why this appears twice.
Any help will be appreciated.