有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

服务打印作业已提交到打印机,但未打印任何内容。JAVA

我对Java打印服务有问题。我需要在默认打印机上打印一个简单的文本文档。我在Windows计算机上使用HP Deskjet作为打印机,安装了所有驱动程序。这是我使用的源代码:

import java.io.*;
import javax.print.*;

public class PrintTest {
 public static void main(String[] args) throws IOException {
  File file = new File("print.txt");
  InputStream is = new BufferedInputStream(new FileInputStream(file));

  //Discover the default print service.
  PrintService service = PrintServiceLookup.lookupDefaultPrintService();

  //Doc flavor specifies the output format of the file.
  DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;  


  // Create the print job
  DocPrintJob job = service.createPrintJob();
  //Create the Doc
  Doc doc = new SimpleDoc(is, flavor, null);

  //Order to print
  try {
   job.print(doc, null);
  } catch (PrintException e) {
   e.printStackTrace();
  }  

  is.close();
  System.out.println("Printing done....");
 }

}

我可以在打印机队列中看到打印作业数毫秒后才消失。但是什么也没印出来。我听说这是因为JDK1.6中的Java打印服务仍然存在缺陷。但我不完全确定。你知道为什么吗


共 (1) 个答案

  1. # 1 楼答案

    我知道这是一个非常晚的答案,但我有同样的问题,在Windows与PDF(不是文本)。似乎打印机可能无法处理本机PDF,因此接受该作业,但什么也没有发生(也没有错误)。我通过使用第三方库Apache PdfBox解决了这个问题,它工作起来很有魅力

    我通过回答一个类似的问题https://stackoverflow.com/a/39271053/935039编写了一些代码示例