How do I set the size of a printed page in java?
Solution 1
Can you try adding this piece of code and rerun :
Book book = new Book();//java.awt.print.Book
book.append(this, pf);
job.setPageable(book);
instead of
job.setPrintable(this);
Solution 2
Book didn't work for me with my multiple pages example (the result was the printing of only the first page), but it gave me another idea. Instead of :
Book book = new Book();//java.awt.print.Book
book.append(this, pf);
job.setPageable(book);
I tried another overloaded method and it worked for all pages:
PageFormat pf = job.defaultPage();
pf.setPaper(paper);
job.setPrintable(this, pf);
If you want custom paper size, you need to forward it to print method as an argument (which Java will do for you but you must pass it to setPrintable method). Changing PaperFormat after method starts to execute will have an effect only on the second and further pages or not have any effect at all.
Anyway, thanks to you both.
SaintWacko
I began programming in middle school, when I asked for a TI-84 Plus for Christmas so I could write TI-BASIC programs on it. I learned the basics of half a dozen languages through middle and high school, but because I didn't have anything to do with them, I didn't learn much of them. I took two years of university for computer science, where I began learning Java, and that has since become my primary language.
Updated on June 05, 2022Comments
-
SaintWacko almost 2 years
I have written a program that uses the Java print API to print pages from a printer. I believe I have put in code to set the page size to letter, but it still prints on whatever size is default for the printer. Here is my
printPage()
method:public void printPage() { getTot(); PrinterJob job = PrinterJob.getPrinterJob(); PageFormat pf = job.defaultPage(); Paper paper = pf.getPaper(); paper.setSize(8.5 * 72, 11 * 72); paper.setImageableArea(0.5 * 72, 0.0 * 72, 7.5 * 72, 10.5 * 72); pf.setPaper(paper); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { if (cov) try { for (j = 0; j < printPaths.size(); j++) job.print(); cov = false; } catch (PrinterException ex) { System.out.println(ex.getMessage()); } if (summ) try { job.print(); summ = false; } catch (PrinterException ex) { System.out.println(ex.getMessage()); } } }
What am I doing wrong?
-
SaintWacko over 12 yearsWow. That works perfectly. It even prints faster now! Can you explain what that does?
-
mprabhat over 12 yearsGlad it worked for you :), It provides the functionality where your page can have different pageformat, I believe you can provide different pages with different formatting though I have never used it :).
-
Mohammed Falha over 10 yearshave any idea how to solve this stackoverflow.com/questions/19983972/… @mprabhat
-
GameDroids over 8 yearsthanks for the hint with the second page, My changed didn't show any effect on a one page document until setting up the pageformat before executing the print job
-
Onychomys over 2 yearsThe reason that book.append(this, pf) didn't work for a multi page print is that you have to tell Book going in how many pages to expect. So you'd need to do book.append(this, pf, 14) or whatever. If you omit the number of pages, it defaults to just doing the first page.