Sar009
Sar009

Reputation: 2276

How to print without showing printdialog in java

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.

Upvotes: 3

Views: 14693

Answers (2)

Saitama
Saitama

Reputation: 133

Because this answer is on top on google, here is a code example :

public class printWithoutDialog implements printable 
{
    public PrintService findPrintService(String printerName)
    {
        for (PrintService service : PrinterJob.lookupPrintServices())
        {
            if (service.getName().equalsIgnoreCase(printerName))
                return service;
        }

        return null;
    }

    @Override
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException 
    {
        if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        /* User (0,0) is typically outside the imageable area, so we must
        * translate by the X and Y values in the PageFormat to avoid clipping
        */
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        /* Now we perform our rendering */

        g.setFont(new Font("Roman", 0, 8));
        g.drawString("Hello world !", 0, 10);

        return PAGE_EXISTS;
    }

    public printSomething(String printerName)
    {
        //find the printService of name printerName
        PrintService ps = findPrintService(printerName);                                    
        //create a printerJob
        PrinterJob job = PrinterJob.getPrinterJob();            
        //set the printService found (should be tested)
        job.setPrintService(ps);      
        //set the printable (an object with the print method that can be called by "job.print")
        job.setPrintable(this);                
        //call je print method of the Printable object
        job.print();
    }
}

To use Java printing without a dialog, you just need to specify to your PrinterJob what is the print service you want to set. The printService class provides a service to the printer you want. This class implements printable as it is made in Java Tutorials (with dialog). The only difference is on the "printSompething" function, where you can find comments.

Upvotes: 5

minhaz1
minhaz1

Reputation: 743

You can surpress the Print Dialog box by using job.print() instead of the job.printDialog(). However if you want to be able to change the margins and everything else then you need to make use of the Paper and PageFormat classes 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 in PageFormat. You can then go and use the setPrintable() method of PrinterJob class with an object of type Printable and PrintFormat as parameters. But most importantly, the Paper class will allow you to set margins if that's your concern.

Upvotes: 5

Related Questions