java 如何在java中不显示printdialog的情况下进行打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11801159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:25:25  来源:igfitidea点击:

How to print without showing printdialog in java

javaprinting

提问by Sar009

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

我正在创建一个 Java 应用程序,应用程序将在其中打印图片和旁边的一些文本。我在打印时有两台打印机,我会相应地选择。我不会为用户显示打印对话框来选择打印机和其他东西。我的代码如下

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.

如果我不跳过该行boolean ok = job.printDialog();,文本将在我的案例 (20,20) 中提到的位置打印,但如果我跳过该行,我的打印可能会在打印机上更远的点完成 (120, 120) 这意味着我需要一个保证金设置。并给我一个设置打印机的代码。

回答by minhaz1

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 Paperand PageFormatclasses 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 Printableand PrintFormatas parameters. But most importantly, the Paperclass will allow you to set margins if that's your concern.

您可以使用job.print()代替来抑制打印对话框job.printDialog()。但是,如果您希望能够更改边距和其他所有内容,那么您需要使用可以在 java.awt.print.Paper 和 java.awt.print.PageFormat 下找到的PaperPageFormat类。纸张将允许您设置纸张的尺寸并在PageFormat. 然后,您可以使用setPrintable()带有类型PrintablePrintFormat参数的对象的 PrinterJob 类的方法。但最重要的是,Paper如果您担心,该课程将允许您设置边距。

回答by kamui

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.

要在没有对话框的情况下使用 Java 打印,您只需向 PrinterJob 指定要设置的打印服务。printService 类为您想要的打印机提供服务。这个类实现了可打印,因为它是在 Java 教程(带对话框)中制作的。唯一的区别在于“printSompething”函数,您可以在其中找到注释。