Java Applet 可以使用打印机吗? [英] Can a Java Applet use the printer?

查看:35
本文介绍了Java Applet 可以使用打印机吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Applet 能否轻松地将 text/html 打印到标准打印机驱动程序(使用所有常见平台 Win/Mac/Linux)?

Can a Java Applet able to print out text/html easily to standard printer driver(s) (with all common platforms Win/Mac/Linux)?

需要签名吗?

推荐答案

要打印,您需要使用 签名小程序 或者如果未签名小程序尝试打印,用户将被提示询问是否允许权限.

To print you will either need to use Signed Applets or if an unsigned applet tries to print, the user will be prompted to ask whether to allow permission.

以下是一些使用 JEditorPane 打印 HTML 的示例代码:

Here is some sample code for printing HTML using JEditorPane:

public class HTMLPrinter implements Printable{
    private final JEditorPane printPane;

    public HTMLPrinter(JEditorPane editorPane){
        printPane = editorPane;
    }

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex){
        if (pageIndex >= 1) return Printable.NO_SUCH_PAGE;

        Graphics2D g2d = (Graphics2D)graphics;
        g2d.setClip(0, 0, (int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight());
        g2d.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());

        RepaintManager rm = RepaintManager.currentManager(printPane);
        boolean doubleBuffer = rm.isDoubleBufferingEnabled();
        rm.setDoubleBufferingEnabled(false);

        printPane.setSize((int)pageFormat.getImageableWidth(), 1);
        printPane.print(g2d);

        rm.setDoubleBufferingEnabled(doubleBuffer);

        return Printable.PAGE_EXISTS;
    }
}

然后将其发送到打印机:

Then to send it to printer:

HTMLPrinter target = new HTMLPrinter(editorPane);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(target);
try{
    printJob.printDialog();
    printJob.print();
}catch(Exception e){
    e.printStackTrace();
}

这篇关于Java Applet 可以使用打印机吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆