用Java将PDF文件发送到网络打印机

时间:2012-11-22 10:09:30

标签: java networking printing

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

以下是我正在使用的代码:

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)中所建议的那样。它也不起作用,但没有错误消息,作业没有出现在打印机的队列中。

1 个答案:

答案 0 :(得分:-2)

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

此致

相关问题