Hi,
I have a printable class that needs to print some text only on 1 page.
The thing is that when I hit print the printer prints 3 pages:
page 1 - with correct content
page 2 - empty page
page 3 - page with content translated some how
then the printing stops.
Here is the code:
private static final double FACTOR = 2.83;
private double width, height;
private void setupJob(){
// page width and height
width = 75*FACTOR;
height = 50*FACTOR;
job = PrinterJob.getPrinterJob();
pf = new PageFormat();
//PageFormat pf =job.pageDialog(job.defaultPage());
aset = new HashPrintRequestAttributeSet();
// Create a letter sized piece of paper with one inch margins
paper = new Paper();
// resize the paper and the imageable area
paper.setSize(width, height);
paper.setImageableArea(0, 0, width, height);
// set paper for the pageFormat
pf.setPaper(paper);
// set the job printable by calling the painter of this object with the pf pageFormat
job.setPrintable(this, pf);
job.setCopies(1);
// set the media printable area in mm
aset.add(new MediaPrintableArea( 0f, 0f, 75f, 50f, MediaPrintableArea.MM));
try {
job.print(aset);
} catch (PrinterException e) {
e.printStackTrace();
}
}
@Override
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) { // We have only one page
return NO_SUCH_PAGE;
}else{
Graphics2D g2d = (Graphics2D)g;
// no translation needed
// g2d.translate(11.34f, 0);
// scale to fit page size
double sx = 0, sy = 0;
sx = width/(75*4);
sy = height/(50*4);
double scale = Math.min(sx, sy);
g2d.scale(scale,scale);
// draw the elements
for (int i=0; i<4; i++){
texts[i].drawText(g);
}
}
return PAGE_EXISTS;
}
Any help would be appreciated!
Thanks!