加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

无法在Windows 7中使用Java 8打印文本文件

发布时间:2020-12-13 20:12:24 所属栏目:Windows 来源:网络整理
导读:我创建了一个报告并将其导出为文本文件,以便在矩阵打印机中打印,但是作业的结果是空白页.我在ubuntu中也是这样做的,正确打印. 这是 Java bug吗? 这是我向您显示问题的示例代码: public class PrintError extends Application { public static void main(St
我创建了一个报告并将其导出为文本文件,以便在矩阵打印机中打印,但是作业的结果是空白页.我在ubuntu中也是这样做的,正确打印.
这是 Java bug吗?

这是我向您显示问题的示例代码:

public class PrintError extends Application {

    public static void main(String args[]) {
        launch(args);
    }

    public void start(Stage stage) throws PrintException {
        PrinterJob printerJob = PrinterJob.createPrinterJob();
        printerJob.showPrintDialog(stage);
        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        printRequestAttributeSet.add(new Copies(printerJob.getJobSettings().getCopies()));
        printRequestAttributeSet.add(new JobName("test",Locale.getDefault()));
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc mydoc = new SimpleDoc(ClassLoader.class.getResourceAsStream("/should-be-printed.txt"),flavor,null);
        DocPrintJob job = getPrintService(printerJob.getPrinter().getName()).createPrintJob();
        job.print(mydoc,printRequestAttributeSet);
    }

    private PrintService getPrintService(String name) {
        for (PrintService printService : java.awt.print.PrinterJob.lookupPrintServices()) {
            if (name.equalsIgnoreCase(printService.getName())) {
                return printService;
            }
        }
        return null;
    }
}

此示例在JavaFx 8中创建,并在Windows 7中的Java build 1.8.0-b132中运行.
我也在github创建了一个简单的项目

从 documentation:

Recommended DocFlavors

The Java Print Service API does not define any mandatorily supported DocFlavors. …

当您有一个PrintService实例时,您可以使用getSupportedDocFlavors()方法来查找它支持哪些风格.

当你发现没有一个DocFlavor. INPUT_STREAM. TEXT_PLAIN_ …味道在列表中,它无助于使用AUTOSENSE,因为这仅仅意味着“最好的猜测”,并且PrintService将不太可能猜到它不支持的类型,而是更有可能数据将被误解为支持的格式之一.

在我的Windows机器上,没有一个提供的PrintServices支持打印明文…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读