Ejemplo:
import java.io.*;
import javax.print.*;
public class PrintTest {
//we are going to print "printtest.txt" file which is inside working directory
//Discover the default print service. If you call PrintServiceLookup.lookupPrintServices
//then it will return an array of print services available and you can choose a
//printer from them
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
//Doc flavor specifies the output format of the file (Mime type + Char encoding)
//You can retrieve doc flavors supported by the printer, like this
//DocFlavor [] supportedFlavors = service.getSupportedDocFlavors();
DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;
// Create the print job
DocPrintJob job = service.createPrintJob();
//Create the Doc. You can pass set of attributes(type of PrintRequestAttributeSet) as the
//3rd parameter specifying the page setup, orientation, no. of copies, etc instead of null.
Doc doc = new SimpleDoc(is, flavor, null);
//Order to print, (can pass attributes instead of null)
try {
job.print(doc, null);
} catch (PrintException e) {
e.printStackTrace();
}
//DocPrintJob.print() is not guaranteed to be synchronous. So it's better to wait on completion
//of the print job before closing the stream. (See the link below)
is.close();
System.
out.
println("Printing done...."); }
}
Leer más:
http://kalanir.blogspot.com/2010/10/how-to-print-from-java-jps-javaxprint.html