0

I want a java program for windows in which I can also send the print specification like Layout Orientation,Number of copies,Pages from and to,etc along with the file path to be printed.

M using This Code ,It works bt I can't provide the print Specifications?

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class PrintFile {    

  public static void fileToPrint(File fis) {
    try {
      Desktop desktop = null;
      if (Desktop.isDesktopSupported())
      {               
        desktop = Desktop.getDesktop();          
      }   
       desktop.print(fis);    
       System.out.print("Printing Document");
    }
    catch (IOException ioe)
    {
      ioe.printStackTrace();
    }
  }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Harinder
  • 11,776
  • 16
  • 70
  • 126

1 Answers1

1

Check out Java Print Service API The javax.print.attribute and javax.print.attribute.standard packages define print attributes which describe the capabilities of a print service, specify the requirements of a print job, and track the progress of the print job.

For example, if you would like to use A4 paper format and print three copies of your document you will have to create a set of the following attributes implementing the PrintRequestAttributeSet interface:

PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(MediaSizeName.ISO_A4); 
attr_set.add(new Copies(3)); 

Then you must pass the attribute set to the print job's print method, along with the DocFlavor.

MediaSize.ISO.A4 or MediaSize.ISO_A4 doesn't work. Instead MediaSizeName.ISO_A4 is correct.

Community
  • 1
  • 1
Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
  • 1
    attr_set.add(MediaSize.ISO_A4); is giving error..says it cnnot be resolved?? – Harinder Apr 21 '11 at 09:19
  • What kind of errors? You will have to import MediaSize class http://download.oracle.com/javase/1.4.2/docs/api/javax/print/attribute/standard/MediaSize.html – Piyush Mattoo Apr 21 '11 at 16:18