How to set DocFlavor for printservice in Java for WIndows.
Posted
by om
on Stack Overflow
See other posts from Stack Overflow
or by om
Published on 2009-09-17T11:04:27Z
Indexed on
2010/04/08
23:03 UTC
Read the original article
Hit count: 328
java
Hello, I have written code to run the printer programatically in Java.I have to set the value of docflavor to print the .txt file.When I set it for INPUT_STREAM.TEXT_PLAIN_UTF_8 and run my program on Linux , it prints the text file.but when i run the same code for windows.It can't print the text file.It only prints the jpeg format file.I am using HP Deskjet F735 printer for this. Following is my code .
DocFlavor docflavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(new Copies(2));
PrintService[] service = PrintServiceLookup.lookupPrintServices(docflavor, attr_set);
if (service.length==0) {
JOptionPane.showMessageDialog(null, "No Printer Selected");
}
else if (service.length > 0) {
System.out.println("Selected printer is " + service[0].getName());
DocPrintJob pj = service[0].createPrintJob();
{
PrintService ps = pj.getPrintService();
FileInputStream fis = null;
try {
File file = new File("/home/ignu/Desktop/newfile");
fis = new FileInputStream(file);
Doc doc = new SimpleDoc(fis, docflavor, null);
pj.print(doc, attr_set);
© Stack Overflow or respective owner