如何在Java中为WIndows设置DocFlavor for printservice

时间:2009-09-17 11:04:28

标签: java

我已编写代码以Java格式编程运行打印机。我必须设置docflavor的值来打印.txt文件。当我为INPUT_STREAM.TEXT_PLAIN_UTF_8设置它并在Linux上运行我的程序时,它会打印文本文件。但是当我为windows运行相同的代码时。它无法打印文本文件。它只打印jpeg格式文件。我正在使用HP Deskjet F735打印机。以下是我的代码。

            DocFlavor docflavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;

            PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();

            attr_set.add(new Copies(2));

  PrintService[] service = PrintServiceLookup.lookupPrintServices(docflavor, attr_set);

             if (service.length==0) {
                    JOptionPane.showMessageDialog(null, "No Printer Selected");
                }

            else if (service.length > 0) {
                System.out.println("Selected printer is " + service[0].getName());

                    DocPrintJob pj = service[0].createPrintJob();
                    {
                        PrintService ps = pj.getPrintService();
                        FileInputStream fis = null;
                        try {
                            File file = new File("/home/ignu/Desktop/newfile");
                            fis = new FileInputStream(file);
                            Doc doc = new SimpleDoc(fis, docflavor, null);
                            pj.print(doc, attr_set);

1 个答案:

答案 0 :(得分:0)

PrintService.getSupportedDocFlavors会为您返回什么?也许UTF-8不受支持?

编辑:

“text / plain; charset = utf-8”似乎不受支持。试试

DocFlavor docflavor = new DocFlavor.INPUT_STREAM ("application/octet-stream") 

代替。