如何使用Java在网络打印机上打印? [英] How to use Java to print on a network printer?

查看:137
本文介绍了如何使用Java在网络打印机上打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Java, I need to print on network printer, which is not installed locally. 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打印将无法通过打印机找到打印机如果路径未注册到运行打印应用程序的Windows / Active Directory用户,则为路径。您必须通过Windows设备和打印机将打印机路径注册为该用户的打印机,以使其可见。然后你必须运行 lookupPrintServices 来查看可用的打印机列表,并按照确切名称<$ c检索正确的 PrintService $ c>字符串已列出。

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. You must register the printer path through Windows "Devices and Printers" as a printer for that user for it to be visible. Then you must run lookupPrintServices to see the list of printers available and retrieve the proper PrintService by the exact name String listed.

/**
 * 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;
        }
    }
}

这篇关于如何使用Java在网络打印机上打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆