程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何指定要在Java中使用的打印机?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何指定要在Java中使用的打印机??

开发过程中遇到如何指定要在Java中使用的打印机?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何指定要在Java中使用的打印机?的解决方法建议,希望对你解决如何指定要在Java中使用的打印机?有所启发或帮助;

PrintUtility下面创建类,将其导入,然后PrintUtility.findPrintservice("name_of_my_printer");在知道打印机名称的情况下尝试调用;如果您不知道可以访问哪些打印机,请调用PrintUtility.getPrinterservicenameList();一个List包含所有可行的注册打印机名称的。

也可以查看我对这个SO问题的答案以获取更多详细信息:

package com.stackoverflow.print;

import java.awt.print.PrinterJob;
import javax.print.Printservice;
import java.util.List;
import java.util.ArrayList;

public final class PrintUtility {

    /**
     * RetrIEve a Print service with a name containing the specifIEd Printername; will return null if not found.
     * 
     * @return
     */
    public static Printservice findPrintservice(String printerName) {

        printername = printername.tolowerCase();

        Printservice service = null;

        // Get array of all print services
        Printservice[] services = PrinterJob.lookupprintservices();

        // RetrIEve a print service from the array
        for (int index = 0; service == null && index < services.length; index++) {

            if (services[index].getname().tolowerCase().indexOf(printerName) >= 0) {
                service = services[index];
            }
        }

        // Return the print service
        return service;
    }

    /**
     * RetrIEves a List of Printer service names.
     * 
     * @return List
     */
    public static List<String> getPrinterservicenameList() {

        // get List of all print services
        Printservice[] services = PrinterJob.lookupprintservices();
        List<String> List = new ArrayList<String>();

        for (int i = 0; i < services.length; i++) {
            List.add(services[i].getname());
        }

        return List;
    }

    /**
     * Utility class; no construction!
     */
     private PrintUtility() {}
}

解决方法

当前正在检索机器上安装的默认打印机以进行打印。我希望能够选择文档要使用的打印机。最好的方法是什么?

码:

 Printservice[] services =
                PrintserviceLookup.lookupPrintservices(psInFormat,null);
        System.out.println("Printer SELEcted " + services[Printerinx]);

        //Printservice defaultservice = PrintserviceLookup.lookupDefaultPrintservice();

        DocFlavor[] docfalvor = services[Printerinx].getSupportedDocFlavors();
        for (int i = 0; i < docfalvor.length; i++) {
            System.out.println(docfalvor[i].getMimeType());
        }
@H_675_30@@H_675_30@
@H_675_30@

大佬总结

以上是大佬教程为你收集整理的如何指定要在Java中使用的打印机?全部内容,希望文章能够帮你解决如何指定要在Java中使用的打印机?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: