// Arranging to have a file printed.

import java.io.FileInputStream;
import java.io.FileNotFoundExcetion;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.Sides;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.javax.print.PrintServiceLookup;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
...

// Get a handle to read the stream of PostScript commands we
// have previously generated.
FileInputStream psStream;
try
   {
   psStream = new FileInputStream( "file.ps" );
   }
catch ( FileNotFoundException e )
   {
   // we can't find the file. Give up.
   return;
   }

// the format is PostScript
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.POSTSCRIPT;

// combine the the stream and the format
Doc myDoc = new SimpleDoc( psStream, psInFormat, null );

// build a Set of Objects describing the special handling we want
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

// we want 5 copies of each page.
aset.add( new Copies(5) );

// we want ISO standard size A4 paper (slightly smaller than 8.5x11)
aset.add( MediaSize.A4 );

// we want two-sided printing
aset.add( Sides.DUPLEX );

// Look for printers that can can handle our format and special handling requests
PrintService[] services = PrintServiceLookup.lookupPrintServices( psInFormat, aset );

if ( services.length > 0 )
   {
   // take the first suitable printer.
   DocPrintJob job = services[0].createPrintJob();
   try
      {
      // hand the stream, format and special handling requests to that printer.
      job.print( myDoc, aset );
      }
   catch ( PrintException e )
      {
      }
   }