简体   繁体   English

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

[英]How to use Java to print on a network printer?

Using Java, I need to print on network printer, which is not installed locally.使用Java,我需要在本地未安装的网络打印机上打印。 I only know the printer name.我只知道打印机名称。 All tutorials I've seen started with something like:我见过的所有教程都是从以下内容开始的:

PrintService []services = PrinterJob.lookupPrintServices();

The problem is there can be no printers installed, so the services will be empty in that case.问题是可能没有安装打印机,因此在这种情况下服务将是空的。 I need to set the printer name directly, not just enumerate through visible printers.我需要直接设置打印机名称,而不仅仅是通过可见打印机进行枚举。

Java AWT Printing will not find a printer via a path if it is not registered to the Windows / Active Directory user that is running the printing application.如果打印机未注册到运行打印应用程序的 Windows/Active Directory 用户,则 Java AWT 打印将无法通过路径找到打印机。 You must register the printer path through Windows "Devices and Printers" as a printer for that user for it to be visible.您必须通过 Windows“设备和打印机”将打印机路径注册为该用户的打印机才能看到它。 Then, as that user, you must run lookupPrintServices to see the list of printers available and retrieve the proper PrintService by the exact name String listed.然后,作为该用户,您必须运行lookupPrintServices以查看可用打印机的列表并通过列出的确切名称String检索正确的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;
        }
    }
}

在我的情况下,出现身份验证错误,我找不到共享打印机,因为我使用 LocalUser 帐户搜索它们,使用另一个帐户或更改打印机授权我可以找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM