I used this
DocFlavor[] docFalvor = prnSvc.getSupportedDocFlavors();
for (int i = 0; i < docFalvor.length; i++) {
System.out.println(docFalvor[i].getMimeType());
}
to get the DocFlavor of my printer and it returned
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
And m using
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");
}
}
code to print file bt the the services array is empty??
when i pass PrintServiceLookup.lookupPrintServices(null, null);
it returns all the printers available and when i print my doc it gives javax.print.PrintException: Printer is not accepting job...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…