How do I set the size of a printed page in java?

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?

Answers


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);

Need Your Help

JSON Serialization in Swift iOS

ios json serialization swift

How can I serialize JSON in swift? I am trying to serialize using this method, however it is causing EXC_BAD_INSTRUCTION. For downloading JSON data, I am using NSURLConnection.

Twitter API token expiring when using TwitterOAuth

php codeigniter oauth twitter

I'm using the library from MunGell/Codeigniter-TwitterOAuth

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.