Printing from Swing

S

Shaun Donovan

Hello all.

I am new to Java and have a problem when trying to print to an A5 page.
I would like my application to set the default page size and margins
without going through a pageDialog. I have tried to set the pageFormat
and Paper, but if I don't send it through the pageDialog, the sizes are
ignored and the defaults are applied. Here is a snippet of the code that
I have tried. I would like to remove the pf = printJob.pageDialog(pf)
line. Am I missing something?

{
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pf = printJob.defaultPage();
Paper pap = pf.getPaper();
pap.setSize(420, 597); //A5
pap.setImageableArea(36, 36, 348, 525); //Margins at 0.5in
pf.setPaper(pap);
pf = printJob.pageDialog(pf);
printJob.setPrintable(this, pf);
if (printJob.printDialog())
{
try
{
printJob.print();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top