R
ruds
Hi,
I want to print document to a specific printer using Java PrintService
API.
When I execute the program I get the output as "No printer services
are available".
My code is:
import java.io.*;
import java.io.File;
import java.io.IOException;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
public class Test {
public static void main(String[] a) {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset= new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));
/* locate a print service that can handle it
*/
PrintService[] pservices =PrintServiceLookup.lookupPrintServices
(flavor, aset);
if (pservices.length == 0)
System.out.println("no printer service available");
if (pservices.length > 0)
{
System.out.println("selected printer " +pservices[0].getName());
//Appendix A Example: PrintPS.java 43
/* create a print job for the chosen service
*/
DocPrintJob pj = pservices[0].createPrintJob();
try {
/* * Create a Doc object to hold the print data.
* Since the data is postscript located in a disk file,
* an input stream needs to be obtained
* BasicDoc is a useful implementation that will if
* requested close the stream when printing is completed.
*/
FileInputStream fis = new FileInputStream("C:\\1.doc");
Doc doc = new SimpleDoc(fis, flavor, null);
/* print the doc as specified
*/
// pj.print(doc, aset);
} catch (IOException ie) {System.err.println(ie);}
/*catch (PrintException e) {System.err.println(e);}*/
}
}
}
I want to print document to a specific printer using Java PrintService
API.
When I execute the program I get the output as "No printer services
are available".
My code is:
import java.io.*;
import java.io.File;
import java.io.IOException;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
public class Test {
public static void main(String[] a) {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset= new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));
/* locate a print service that can handle it
*/
PrintService[] pservices =PrintServiceLookup.lookupPrintServices
(flavor, aset);
if (pservices.length == 0)
System.out.println("no printer service available");
if (pservices.length > 0)
{
System.out.println("selected printer " +pservices[0].getName());
//Appendix A Example: PrintPS.java 43
/* create a print job for the chosen service
*/
DocPrintJob pj = pservices[0].createPrintJob();
try {
/* * Create a Doc object to hold the print data.
* Since the data is postscript located in a disk file,
* an input stream needs to be obtained
* BasicDoc is a useful implementation that will if
* requested close the stream when printing is completed.
*/
FileInputStream fis = new FileInputStream("C:\\1.doc");
Doc doc = new SimpleDoc(fis, flavor, null);
/* print the doc as specified
*/
// pj.print(doc, aset);
} catch (IOException ie) {System.err.println(ie);}
/*catch (PrintException e) {System.err.println(e);}*/
}
}
}