How to print without showing printdialog in java

13,192

Solution 1

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.

Solution 2

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.

Share:
13,192

Related videos on Youtube

Sar009
Author by

Sar009

Updated on June 04, 2022

Comments

  • Sar009
    Sar009 almost 2 years

    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.

    • E_net4 the comment flagger
      E_net4 the comment flagger almost 12 years
      I have a feeling this question is way too specific to that class PrinterJob and the method printDialog(). You will have to provide us more information.
    • Sar009
      Sar009 almost 12 years
      i need code to print without print dialog, setting margins, and selecting printer
    • E_net4 the comment flagger
      E_net4 the comment flagger almost 12 years
      You didn't quite well understand the point. We would need to know ALL the underlying procedures applicable to the PrinterJob and what probably is the Job class. This might not even be part of a public library, so there is no way we can help you under these circumstances.
    • Sar009
      Sar009 almost 12 years
      PrinterJob is in java.awt.print.PrinterJob
    • E_net4 the comment flagger
      E_net4 the comment flagger almost 12 years
      Good thing you pointed that out. I admit I had never heard of it, and couldn't find it in my search.
  • Sar009
    Sar009 almost 12 years
    I am using job.print() to print. please give me code to set margins. I know Paper and PageFormat can be used to achieve that but how
  • minhaz1
    minhaz1 almost 12 years
    I can help assist you to improve your code, but you need to write your own code. If you add some of the code you've worked on I can hint you in the right direction. Here's an Example
  • Kris
    Kris about 9 years
    Improve the utility of your answer by at least explaining briefly what this code does...Just dumping big blocks of code is not the most informative practice I would think.
  • dextermini
    dextermini almost 7 years
    could you please provide me more details and source link, i also want to integrate the same. Is tere any sample code example