Harinder
Harinder

Reputation: 11944

Java Print program with Specifications?

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();
    }
  }
}

Upvotes: 0

Views: 1507

Answers (1)

Piyush Mattoo
Piyush Mattoo

Reputation: 16153

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.

Upvotes: 1

Related Questions