我创建了一个报告并将其导出为文本文件,以便在矩阵打印机中打印,但是,作业的结果是空白页。我在 ubuntu 中做了同样的事情,它打印正确。 是 Java 错误吗?
这是我用来向您展示问题的示例代码:
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 都没有时。输入流。 TEXT_PLAIN_…
flavors 在列表中,使用 AUTOSENSE
没有帮助,因为它只是意味着“最佳猜测”,PrintService
不太可能猜测它不支持的类型,相反,数据更有可能被误解为它支持的格式之一。
在我的 Windows 机器上,提供的 PrintService
都不支持打印明文......
关于java - 无法在 Windows 7 中使用 Java 8 打印文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26663060/