I am creating a java application where application will print a picture and some text beside it. I have two printers while printing I will select accordingly. I will not show the print dialog for the user to select printer and other stuffs. My code is as follow
PrinterJob job = PrinterJob.getPrinterJob();
boolean ok = job.printDialog();
If I don’t skip the line boolean ok = job.printDialog(); the text is being printed at the mentioned position in my case (20,20) but if i skip the line my printing is done at a point further away on the printer maybe (120, 120) this mean i need a margin setup. and also give me a code to set printer.
You can surpress the Print Dialog box by using
job.print()instead of thejob.printDialog(). However if you want to be able to change the margins and everything else then you need to make use of thePaperandPageFormatclasses which can be found under java.awt.print.Paper and java.awt.print.PageFormat. Paper will allow you to set the size of the paper and use it inPageFormat. You can then go and use thesetPrintable()method of PrinterJob class with an object of typePrintableandPrintFormatas parameters. But most importantly, thePaperclass will allow you to set margins if that’s your concern.