2012-11-22 102 views
2

我想发送一个PDF/DOC/ODT文件到使用javax.print连接到LAN的打印机,但它甚至不会将作业发送到打印队列。我试图打印“正常”文件(使用Adobe Reader/Open Office)并且工作完美,因此打印机连接良好。我也尝试从代码发送到虚拟打印机(PDFCreator),它工作。发送PDF文件到网络打印机的Java

这里是我正在使用的代码:

public Boolean Imprimir (String filePath){ 
    Boolean correct = true; 

    FileInputStream psStream = null; 
    try { 
     psStream = new FileInputStream(filePath); 
    } catch (FileNotFoundException ex) { 
     ex.printStackTrace(); 
     correct = false; 
    } 
    if (psStream != null) { 

     DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE; 
     Doc myDoc = new SimpleDoc(psStream, psInFormat, null);  
     PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();    
     PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset); 

     PrintService myPrinter = null; 
     for (int i = 0; i < services.length; i++){ 
      String svcName = services[i].toString();    

      if (svcName.contains("Xerox")){ 
       myPrinter = services[i];      
       break; 
      } 
     } 

     if (myPrinter != null) {    
      DocPrintJob job = myPrinter.createPrintJob(); 
      try{      
       job.print(myDoc, aset); 
      } 
      catch(PrintException ex){ 
       ex.printStackTrace(); 
       correct = false; 
      } 
     } else { 
      System.out.println("No printer services found"); 
      correct = false; 
     } 
    } 
    else{ 
     correct = false; 
    } 

    return correct; 
} 

打印机使用LPR协议连接。

在此先感谢

编辑:我也尝试使用jLpr,如其他职位(Java printing directly to a Postscript network printer)建议。它也没有工作,但没有错误信息,作业没有出现在打印机的队列中。

+0

我发现问题可能是打印机使用LPR协议连接。我尝试将打印作业发送到与RAW协议连接的打印机,并且工作正常。我不会回答这个问题,因为我仍然不知道如何处理LPR协议。 – Joel

+0

你解决了你的问题吗?你介意分享这个解决方案吗?谢谢 –

回答

-2

您使用的是什么Adobe Reader版本?可能是Adobe Reader安全问题,具体取决于其版本。

Regards

相关问题