Hey I'm trying to make an invoice software everything is fine while I'm printing bill then it is printing according to standard printer size but I want to print it through billing printer I'm not using jesper report or any other reporting software I'm just print JFrame by swing code.
Here is my code. . .
final Component comp;
public testing(Component comp){
this.comp=comp;
}
@Override
public int print(Graphics g, PageFormat format, int page_index)
throws PrinterException {
if (page_index > 0) {
return Printable.NO_SUCH_PAGE;
}
// get the bounds of the component
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// get the bounds of the printable area
double pHeight = format.getImageableHeight();
double pWidth = format.getImageableWidth();
double pXStart = format.getImageableX();
double pYStart = format.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
Graphics2D g2 = (Graphics2D) g;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
comp.paint(g2);
return Printable.PAGE_EXISTS;
}
//_____________________________________pint Method
public static void printing(){
PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat preformat = pjob.defaultPage();
preformat.setOrientation(PageFormat.PORTRAIT);
PageFormat postformat = pjob.defaultPage();
//If user does not hit cancel then print.
if (preformat != postformat) {
//Set print component
pjob.setPrintable(new testing(frame), postformat);
// if (pjob.printDialog()) {
try {
pjob.print();
} catch (PrinterException ex) {
Logger.getLogger(testing.class.getName()).log(Level.SEVERE, null, ex);
}
//}
}
}
Is there any Idea how to print it fit the printer roll because there is no any option to print paper fit to printing roll paper.
Thnkx in advance.