Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java print service

Status
Not open for further replies.

yytan

Programmer
May 8, 2002
113
MY
dear all;

i am getting this code from sun website but it seem like doesnt print anything out? when i compile, it gives no error, may i know why?


import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;

public class PrintPS {

public static void main(String args[]) {
PrintPS ps = new PrintPS();
}
public PrintPS() {
/* Construct the print request specification.
* The print data is Postscript which will be
* supplied as a stream. The media size
* required is A4, and 2 copies are to be printed
*/
//DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;
//DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;
PrintRequestAttributeSet aset =
new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));
//aset.add(Sides.TWO_SIDED_LONG_EDGE);
aset.add(Finishings.STAPLE);

/* locate a print service that can handle it */
PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(flavor, aset);
System.out.println("pservices len = " + pservices.length);
if (pservices.length > 0) {
System.out.println("selected printer " + pservices[0].getName());

/* 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("CAM10310.txt");
Doc doc = new SimpleDoc(fis, flavor, null);

/* print the doc as specified */
pj.print(doc, aset);

/*
* Do not explicitly call System.exit() when print returns.
* Printing can be asynchronous so may be executing in a
* separate thread.
* If you want to explicitly exit the VM, use a print job
* listener to be notified when it is safe to do so.
*/

} catch (IOException ie) {
System.err.println(ie);
} catch (PrintException e) {
System.err.println(e);
}
}
}
}

 
Seems to be a common problem, there's a lot of debate on Sun's Forum pages, but no answers!

I think its down to what flavors your printer supports. try using


DocFlavor[] flavors = pservices[0].getSupportedDocFlavors();
for(int i=0;i<flavors.length;i++){
System.out.println(&quot;-------------------------------------------------------------&quot;);
System.out.println(&quot;MediaSubtype = &quot; +flavors.getMediaSubtype());
System.out.println(&quot;MediaType = &quot; +flavors.getMediaType());
System.out.println(&quot;MimeType = &quot; +flavors.getMimeType());
System.out.println(&quot;RepresentationClassName = &quot; +flavors.getRepresentationClassName());
System.out.println(&quot;hostEncoding = &quot; +flavors.hostEncoding);
System.out.println(&quot;toString = &quot; +flavors.toString());
}


to list the flavors, I note on mine (Samsung ML1200) it doesn't list a text/plain. ;-(

good luck, I'm curious what the answer is myself.
 
OOps forgot, can't use [ i ] unless surround text with [ code ] [/ code ] as this converts text to italics... Try again..


Seems to be a common problem, there's a lot of debate on Sun's Forum pages, but no answers!

I think its down to what flavors your printer supports. try using

Code:
      DocFlavor[i] flavors = pservices[0].getSupportedDocFlavors();
      for(int i=0;n<flavors.length;i++){
        System.out.println(&quot;-------------------------------------------------------------&quot;);
        System.out.println(&quot;MediaSubtype            = &quot; +flavors[i].getMediaSubtype());
        System.out.println(&quot;MediaType               = &quot; +flavors[i].getMediaType());
        System.out.println(&quot;MimeType                = &quot; +flavors[i].getMimeType());
        System.out.println(&quot;RepresentationClassName = &quot; +flavors[i].getRepresentationClassName());
        System.out.println(&quot;hostEncoding            = &quot; +flavors[i].hostEncoding);
        System.out.println(&quot;toString                = &quot; +flavors[i].toString());
      }

to list the flavors, I note on mine (Samsung ML1200) it doesn't list a text/plain. ;-(

good luck, I'm curious what the answer is myself.
 
hi thekobbler;

thanks for ur suggestion, i will go around and try it out. what make me more harder is i am gonna to print in a dot matrix printer using fan-fold paper.... dont know how ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top