Hi, I'm trying to use the PageFormat information to modify my javax.swing based printout prior to printing it. I am stumped as to how I can get the PageFormat from the PrintJob (which is obtained using getPrinterJob() and printDialog()). I know there is the getPageFormat method, but I can't figure out how to get the PrintRequestAttributeSet (which is not the printJob.getPrintService().getPrintAttributes()). Honestly, all I really want to know is the width and height of the page. Any ideas on how I can do that? Thanks.
A:
If you just need the page width and height, I think PageFormat.getImageableWidth() and
PageFormat.getImageableHeight() are what you're looking for.
Sorry, that's not what you wanted. How about this for a hack:
printable.setFakeJob(true);
job.print();
printable.setFakeJob(false);
job.print();
and in your printable,
public int print(Graphics graphics, PageFormat pf, int pageIndex) {
if (isFakeJob()) {
/* use passed-in PageFormat to set up whatever you need to set up */
return NO_SUCH_PAGE;
} else {
/* do your actual printing */
}
}
Not pretty, admittedly.
that's the problem, I can't get the PageFormat for the print job.
2009-04-27 03:06:34
Sorry, managed to misread your question! I understand now, I think: you want to get the page size *before* your Printable.print(Graphics, PageFormat, int) method is called back. Unfortunately I can't see a way to do it either: PrinterJob doesn't seem to let you access the choices the user makes in the print dialog without calling PrinterJob.print().I have an idea for a hack which I'll post as another answer.
2009-04-28 02:46:23
that is ugly, but it does work.
2009-04-29 01:16:42