1

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...

Cœur
  • 37,241
  • 25
  • 195
  • 267
Harinder
  • 11,776
  • 16
  • 70
  • 126
  • While I try to run your code in Eclipse, one suggestion : do not eat your exceptions. Print or log them. If you keep eating exceptions, you'll never know what is going on. Notice the empty catch blocks. Do not use try/catch simply because the code wont compile without them. – lobster1234 Apr 25 '11 at 06:27
  • Its giving javax.print.PrintException: Printer is not accepting job. – Harinder Apr 25 '11 at 06:35
  • So the array is not empty anymore? – lobster1234 Apr 25 '11 at 06:37
  • this happens when i pass PrintServiceLookup.lookupPrintServices(null, null); – Harinder Apr 25 '11 at 06:38
  • else when i pass PrintServiceLookup.lookupPrintServices(psInFormat, aset); the array is empty – Harinder Apr 25 '11 at 06:39
  • I think that its empty when you pass aset is because no printer is configured to accept those printing attributes. Try to use something compatible. When you pass null, the printer rejects the job which is what is going on. Try to list the printer attributes and work with the supported set. – lobster1234 Apr 25 '11 at 06:53
  • I'd recommend checking out the following for a more thorough treatment of the issue: [Printing with Attributes(Tray Control, Duplex, etc...) using javax.print library](http://stackoverflow.com/questions/14328012/printing-with-attributestray-control-duplex-etc-using-javax-print-library) – amaidment Sep 30 '15 at 07:49

1 Answers1

3

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.

Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56