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

Way to implement “Get all strings with Levenshtein distance less than X”

data-structures levenshtein-distance

I'm wondering whether there's an efficient data structure to perform "Retrieve all strings with levenshtein distance less than X".

How to parse an unknown file type?

c# file parsing model

So, this is a general question which doesn't require a direct solution, but maybe some advice or tutorial information. What I am trying to achieve is a way to be able to read this unknown model typ...

Mongoid complex ploymorphic relationship

ruby-on-rails polymorphism mongoid polymorphic-associations

I would like to set up a set of relationships that look something like the code below