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

bre*_*leq 15 java printing javafx java-8

我创建了一个报告并将其导出为文本文件,以便在矩阵打印机中打印,但是,作业的结果是一个空白页面.我在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Hol*_*ger 8

文档:

推荐的DocFlavors

Java Print Service API未定义任何强制支持的DocFlavor....

当您有PrintService实例时,可以使用该方法getSupportedDocFlavors()找出它支持的风格.

当你发现DocFlavor. INPUT_STREAM. TEXT_PLAIN_…列表中没有任何一种风味时,它没有用处,AUTOSENSE因为它只是意味着"最佳猜测"而且它不太可能PrintService猜测它不支持的类型,相反,它更可能是数据将被误解为它支持的格式之一.

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