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

前端之家收集整理的这篇文章主要介绍了无法在Windows 7中使用Java 8打印文本文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个报告并将其导出为文本文件,以便在矩阵打印机中打印,但是作业的结果是空白页.我在ubuntu中也是这样做的,正确打印.
这是 Java bug吗?

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

  1. public class PrintError extends Application {
  2.  
  3. public static void main(String args[]) {
  4. launch(args);
  5. }
  6.  
  7. public void start(Stage stage) throws PrintException {
  8. PrinterJob printerJob = PrinterJob.createPrinterJob();
  9. printerJob.showPrintDialog(stage);
  10. PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
  11. printRequestAttributeSet.add(new Copies(printerJob.getJobSettings().getCopies()));
  12. printRequestAttributeSet.add(new JobName("test",Locale.getDefault()));
  13. DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
  14. Doc mydoc = new SimpleDoc(ClassLoader.class.getResourceAsStream("/should-be-printed.txt"),flavor,null);
  15. DocPrintJob job = getPrintService(printerJob.getPrinter().getName()).createPrintJob();
  16. job.print(mydoc,printRequestAttributeSet);
  17. }
  18.  
  19. private PrintService getPrintService(String name) {
  20. for (PrintService printService : java.awt.print.PrinterJob.lookupPrintServices()) {
  21. if (name.equalsIgnoreCase(printService.getName())) {
  22. return printService;
  23. }
  24. }
  25. return null;
  26. }
  27. }

此示例在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支持打印明文…

猜你在找的Windows相关文章