带有规格问题的Java Print程序?

时间:2011-04-25 06:10:22

标签: java printing

我用过这个

DocFlavor[] docFalvor = prnSvc.getSupportedDocFlavors();
        for (int i = 0; i < docFalvor.length; i++) {
            System.out.println(docFalvor[i].getMimeType());
        }

获取我的打印机的DocFlavor并返回

image/gif
image/gif
image/gif
image/jpeg
image/jpeg
image/jpeg
image/png
image/png
image/png
application/x-java-jvm-local-objectref
application/x-java-jvm-local-objectref
application/octet-stream
application/octet-stream
application/octet-stream

使用

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Sides;


public class PrintFileWithSpec {


    public static void printFile(String filename){

    FileInputStream psStream=null;
    try {
       psStream = new FileInputStream(filename);
    } catch (FileNotFoundException ffne) {}

    if (psStream == null) {
        return;
    }

    DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc myDoc = new SimpleDoc(psStream, psInFormat, null);  
    PrintRequestAttributeSet aset = 
        new HashPrintRequestAttributeSet();
    aset.add(new Copies(5));
    //aset.add(MediaSize.ISO_A4); 
    aset.add(Sides.DUPLEX);
    PrintService[] services = 
      PrintServiceLookup.lookupPrintServices(psInFormat, aset);

    DocFlavor[] docFalvor = services[3].getSupportedDocFlavors();
    for (int i = 0; i < docFalvor.length; i++) {
        System.out.println(docFalvor[i].getMimeType());
    }

    if (services.length > 0) {
       DocPrintJob job = services[3].createPrintJob();
       try 
       {
           job.print(myDoc, aset);
       } catch (PrintException pe) {
  System.out.print(pe);

}
        }
        }

    public static void main(String [] args)
    {       
        printFile("D:/Resume.doc");
    }
}

打印文件的代码bt,services数组是空的吗?

当我传递PrintServiceLookup.lookupPrintServices(null,null);

它会返回所有可用的打印机,当我打印我的文档时,它会提供javax.print.PrintException:打印机不接受作业......

1 个答案:

答案 0 :(得分:3)

尝试删除并添加打印机。此外,如果Docflavor是Autosense,您传递的属性集将不会生效。您要打印什么类型的文档?它是受支持的Docflavors之一吗?如果docflavor不支持,则无法使用Java Print Service API打印doc,pdf等。