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

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

问题描述

可以在Java小程序能够轻松地打印出的文本/ HTML标准的打印机驱动程序(S)(与所有常见平台赢/苹果/ Linux)的?

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

是否需要签名?

推荐答案

要打印你要么需要使用的或如果未签名的Applet尝试打印,用户将被提示HTML>签名的小程序询问是否允许的权限。

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.

下面是一些示例code打印使用的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小程序可以使用打印机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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