Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Java Print/Printer set page margin problem

0 views
Skip to first unread message

Andrew Chau

unread,
Jun 20, 2002, 5:09:41 PM6/20/02
to
I am writing a Java Swing app, and I ran into a problem when the app invoke
the print routing, the output from the printer always start out with 1 inch
margin on the left.

Is there any way to programmatically change the PageFormat or other
properties in java?

Thanks

Andrew


Linda Radecke

unread,
Jun 21, 2002, 1:52:33 AM6/21/02
to

Andrew Chau wrote:


You can create a PageFormat with a Paper, for instance, and call
the PrinterJob.validatePage(PageFormat) method, which returns a
PageFormat; and you can set your margins then accordingly. E.g.
I use this in one of my programs:

PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat pform = getPageFormat(pjob);

....

private PageFormat getPageFormat(PrinterJob pjob) {

PageFormat pform = pjob.defaultPage();

customizePaper(pform);
/* some information */
PrintInfo.out(" after setting paper ", pform);

return pjob.validatePage(pform);
}

private void customizePaper(PageFormat pform) {

Paper paper = pform.getPaper();

paper.setImageableArea(0, 5, paper.getWidth(),
paper.getHeight() - 2 * 5);
pform.setPaper(paper);
}

public int print(....){.....}


Just to give you an idea, this can be differently of course,
and not all printers support this well, for instance I get a
bit different result on different printers,a description can
be found in the FAQ, too:

http://java.sun.com/products/java-media/2D/forDevelopers/java2dfaq.html#margin

Linda
--
\ / To where the water and the earth caress and the down of a
- * - peach says mmh, yes,do I look for those millionaires like
/ \ a Machiavellian girl would when I could wear a sunset?
Kate Bush, The Sensual World

0 new messages