Remove Printer Dialog - PrintJob Java

3

In a Java application I print through PrintJob, but the way I print when I call the Print method, it opens a printer dialog so I can choose which one to print, I tried and could not do with which to print without having to call the dialog.

Follow my printing method below.

Print.java

public void imprimir() {


    Frame f = new Frame("Frame temporário");
    f.setSize((int) 283.46, 500);
    f.pack();


    Toolkit tk = f.getToolkit();

    PrintJob pj = tk.getPrintJob(f, "MP4200", null);



    if (pj != null) {
        Graphics g = pj.getGraphics();

    ...Aqui vai os dados impressos...


        g.dispose();

        pj.end();
    }


    f.dispose();
}
    
asked by anonymous 22.05.2015 / 13:58

1 answer

0

You may be trying to adapt this example that I made friend to your need. In it I print an object of the Drawing class that should implement Printable to be a printable object, then I call the print () method in the main class of the PrinterJob object , so it will print directly to the impessor, without displaying the dialog, when you try to print from a PrinterJob from a Toolkit will always display the dialog box of printers, there is no way to do that.

Object class to be printed.


public class Desenho implements Printable {
    // Deve implementar Printable para que seja um objeto imprimivel

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
            throws PrinterException {
        if (pageIndex > 0) {
            return Printable.NO_SUCH_PAGE;
        } else {
            // Renderiza um quadrado
            Graphics2D g2d = (Graphics2D) graphics;
            int x = 90;
            int y = 90;
            g2d.draw(new Rectangle2D.Double(x, y, 500, 500));

            // Mostra que imprimiu o objeto
            return Printable.PAGE_EXISTS;
        }
    }
}   

And the test class %pr_e%

public class Impressora {

}    

    
02.06.2015 / 14:48