2012-01-19 66 views
4

我对Java打印服务问题。我需要打印一个简单的文本文档,到我的默认打印机。我在Windows机器上使用HP Deskjet作为我的打印机,安装了所有驱动程序。这是源代码,我使用:打印作业提交到打印机,但没有打印任何东西。 Java的

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...."); 
} 

} 

我可以看到打印机队列中的打印作业时前的走了几个milisecond。但没有打印。我听说这是因为在JDK 1.6的Java打印服务仍然是越野车。但我不完全确定。任何想法为什么?

+0

请让我知道如果你发现任何解决方案,我面临同样的问题。给你的回应[这里](http://stackoverflow.com/questions/15810865/print-current-html-page-on-printer-from-java-bean-in-jsf) –

回答

0

我知道这是一个很迟的答案,但我有同样的问题在Windows上使用PDF文件(不是文本)。看来,打印机可能无法处理原生PDF文件,所以这项工作被接受,但没有任何反应(没有错误太)。我解决了这个利用第三方的lib,Apache PdfBox,和它的工作就像一个魅力。

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