Saturday, August 8, 2009

Printing from Java Program

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;


public class TestPrinter
{
public static void main(String[] args)
{

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
int printnbr = 0;
DocPrintJob pj = pservices[printnbr].createPrintJob();
try
{
FileInputStream fis = new FileInputStream("C:\\YServer.txt");
Doc doc = new SimpleDoc(fis, flavor, null);
pj.print(doc, aset);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}

No comments:

Post a Comment