Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
782 views
in Technique[技术] by (71.8m points)

printing - Java Print program with Specifications issues?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try removing and adding the printer. Also, the attribute set you pass won't take in effect if the Docflavor is Autosense. What type of document are you trying to print? Is it among the supported Docflavors? You cannot print doc, pdf etc using Java Print Service API if it is not supported by the docflavor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...