Hello! I am developing a POS System. I have need to print Sales receipt. I have no physical printer to test my printing but i have print on xps document. I am facing a problem with indentation of my data on Sales receipt. Following is the code which i wrote to print my recipt:
@Override
public int print(Graphics graphics,
PageFormat pageFormat, int pageIndex)
throws PrinterException {
if(pageIndex > 0){
return NO_SUCH_PAGE;
}
Font f;
int y = 15;
int rowCount = model.getRowCount();
String s = String.format("%s %30s %30s
%30s", "item","Qty","Price","Amount");
String s2 = String.format("%s %30s %30s
%30s","-----" ,"-----","-----","-----" );
f = new Font(Font.SANS_SERIF,Font.PLAIN,
8);
graphics.setFont(f);
Graphics2D g2D = (Graphics2D)graphics;
g2D.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
graphics.drawString(s, 100, y);
y=y+15;
graphics.drawString(s2, 100, y);
// extracting all the values from.
jTable
for(int i=0;i<rowCount;i++){
String s1 =String.format("%s %30s %30s
%30s", model.getValueAt(i, 0),
model.getValueAt(i, 1), model.getValueAt(i,
2), model.getValueAt(i, 3));
y = y+15;
graphics.drawString(s1, 100, y);
}
return PAGE_EXISTS;
}
And below is the code which i wrote under
the
Actionperformed event of my 'Print'
Button
private void.
btnPrintActionPerformed
(java.awt.event.ActionEvent evt) {
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pageFormat =
job.defaultPage();
Paper paper = new Paper();
paper.setSize((double)paper.getWidth(),
(double) (paper.getHeight()));
paper.setImageableArea(20, 20,
paper.getWidth()-20,paper.getHeight()-20);
pageFormat.setPaper(paper);
job.setPrintable(this,pageFormat);
boolean ok = job.printDialog();
if(ok){
try{
job.print();
} catch (PrinterException ex) {
Logger.getLogger(Sale.class.getName()).
log(Level.SEVERE, null, ex);
}
}
else{
JOptionPane.showMessageDialog(null,"Job.
did not successfully complete");
}
}
Below is the ouptut on xps document:
Item. Qty. Price. Amount
Rubber. 10. 20. 200
Foor rubber. 20. 100. 2000
Handle grip. 30. 100. 3000
Helmet. 1. 500. 500
As you can see that in the code I have given a space of %30s between every field.
But in output when an item name length extends,the space value remains same(%30s) but the indentation is spoiled. So what should i do
so that my items indentaion remain correct?