Java Applet 可以使用打印机吗?

发布于 2024-07-11 12:42:31 字数 88 浏览 4 评论 0原文

Java Applet 能否轻松地将文本/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)?

Does it need to be signed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-07-18 12:42:31

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

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

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;
    }
}

然后将其发送到打印机:

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

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.

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();
}
沉溺在你眼里的海 2024-07-18 12:42:31

为了进行打印,安全管理器需要允许其访问打印机。 这意味着要么对小程序进行签名,要么至少使用 Sun 的 Java 插件的最新版本如果未签名的小程序尝试打印,系统将提示用户询问是否允许允许。

In order to print, the security manager needs to allow it to access the printer. This means either signing the applet or, at least with recent versions of Sun's Java plugin, if an unsigned applet tries to print, the user will be prompted to ask whether to allow permission.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文