如何使用Java在网络打印机上打印?

如何使用Java在网络打印机上打印?,java,printing,Java,Printing,使用Java,我需要在网络打印机上打印,该打印机不是本地安装的。我只知道打印机的名字。我看过的所有教程都是从以下内容开始的: PrintService[]services=PrinterJob.lookupPrintServices() 问题是无法安装打印机,因此在这种情况下服务将为空。我需要直接设置打印机名称,而不仅仅是通过可见打印机进行枚举。如果打印机未注册到运行打印应用程序的Windows/Active Directory用户,Java AWT打印将无法通过路径找到打印机。必须通过Wind

使用Java,我需要在网络打印机上打印,该打印机不是本地安装的。我只知道打印机的名字。我看过的所有教程都是从以下内容开始的:

PrintService[]services=PrinterJob.lookupPrintServices()


问题是无法安装打印机,因此在这种情况下服务将为空。我需要直接设置打印机名称,而不仅仅是通过可见打印机进行枚举。

如果打印机未注册到运行打印应用程序的Windows/Active Directory用户,Java AWT打印将无法通过路径找到打印机。必须通过Windows“设备和打印机”将打印机路径注册为该用户的打印机,才能使其可见。然后,作为该用户,您必须运行
lookupPrintServices
以查看可用打印机的列表,并按所列的确切名称
字符串
检索适当的
PrintService

/**
 * Retrieve the specified Print Service; will return null if not found.
 * @return
 */
public static PrintService findPrintService(String printerName) {

    PrintService service = null;
    
    // Get array of all print services - sort order NOT GUARANTEED!
    PrintService[] services = PrinterJob.lookupPrintServices();
    
    // Retrieve specified print service from the array
    for (int index = 0; service == null && index < services.length; index++) {
        
        if (services[index].getName().equalsIgnoreCase(printerName)) {

            service = services[index];
        }
    }

    // Return the print service
    return service;
}

/**
 * Retrieve a PrinterJob instance set with the PrinterService using the printerName.
 * 
 * @return
 * @throws Exception IllegalStateException if expected printer is not found.
 */
public static PrinterJob findPrinterJob(String printerName) throws Exception {

    // Retrieve the Printer Service
    PrintService printService = PrintUtility.findPrintService(printerName);

    // Validate the Printer Service
    if (printService == null) {

        throw new IllegalStateException("Unrecognized Printer Service \"" + printerName + '"');
    }
    
    // Obtain a Printer Job instance.
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    
    // Set the Print Service.
    printerJob.setPrintService(printService);

    // Return Print Job
    return printerJob;
}

/**
 * Printer list does not necessarily refresh if you change the list of 
 * printers within the O/S; you can run this to refresh if necessary.
 */
public static void refreshSystemPrinterList() {

    Class[] classes = PrintServiceLookup.class.getDeclaredClasses();

    for (int i = 0; i < classes.length; i++) {

        if ("javax.print.PrintServiceLookup$Services".equals(classes[i].getName())) {

            sun.awt.AppContext.getAppContext().remove(classes[i]);
            break;
        }
    }
}
/**
*检索指定的打印服务;如果未找到,将返回null。
*@返回
*/
公共静态PrintService findPrintService(字符串printerName){
PrintService=null;
//获取所有打印服务的数组-不保证排序顺序!
PrintService[]services=PrinterJob.lookupPrintServices();
//从阵列中检索指定的打印服务
for(int index=0;service==null&&index
在我的情况下,出现身份验证错误,我找不到共享打印机,因为我使用LocalUser帐户搜索它们,使用其他帐户或更改打印机授权,我可以找到它。

可通过'lookupPrintServices()获得的打印机'列出注册到O/S和注册到运行应用程序的用户的打印机。例如,如果在Windows中,您需要确保所需的打印机已注册为应用程序运行时的active directory用户(本地用户、注册服务等)。这是一次性注册。在网络上,您还可以将打印机注册到网络并访问它//服务器/printernami无需使用lookupPrintServices()。我有//server/printername格式的打印机名称。问题是,lookupPrintServices无法看到它们中的任何一个,我无法注册或安装它。在.NET中,您只需在对象printersettings中设置打印机名称。我想知道,Java中是否也存在类似的可能性。很简单。我有pdf文件(处理PDFBox)和打印机名称,我想在不打开Acrobat的情况下以静默方式打印它。我想知道最简单的方法。即使它已注册到网络,如果运行java应用程序的域用户在windows控制面板中看不到打印机,java也无法通过awt打印api看到它。您可能有名称和正确的路径,但如果它未注册到运行应用程序的服务器,则找不到它。因此,您试图告诉我,无法在未注册的打印机上打印?太好了。谢谢。为什么限制您将网络打印机注册到运行应用程序的服务器上?这是一项一次性任务。老实说,我不知道。这是客户网络,他有理由这么做。我知道这很糟糕,但注册打印机不是一个选项。但这是你客户的打印机和服务器,而且。。。您的客户是否了解这类事情的实际工作原理?未注册的打印机(通常)可以使用端口9100上的套接字直接写入。这确实要求您了解打印机需要的低级数据。许多office打印机都可以以本机方式处理PDF(请确保大小正确,PDFBOX使用驱动程序处理大小调整,而使用未注册的打印机可以绕过该驱动程序)。