當我調試這個程序的服務數組是空的?打印機服務未找到?
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.MediaSize;
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.POSTSCRIPT;
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);
if (services.length > 0) {
DocPrintJob job = services[0].createPrintJob();
try
{
job.print(myDoc, aset);
} catch (PrintException pe) {}
}
}
public static void main(String [] args){
printFile("D:/Resume.doc");
}
}
這似乎是在指定的屬性'aset'有點限制太多 - 當我通過'null'時,它對我有用。 – AbdullahC 2011-04-21 09:35:28
那我該怎麼辦? – Harinder 2011-04-21 09:40:33
我通過null而不是一個仍然相同的問題 – Harinder 2011-04-21 09:41:41