A
Arun Kumar Srinivasan
Hi,
I am trying to use the javax.print classes to print some text
(currently as String, but can be modified to read it from a text
file). I am having trouble because it appears as if the printer (HP
LaserJet) doesnt seem to support printing text. I tried to use
DocFlavor.STRING.TEXT_PLAIN as well as
DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST but I got a flavor not
supported error. So, I took the selected printer and asked to display
its supported flavors (Mime type and representation class name) and
this is the result I got:
image/jpeg-->[B
image/jpeg-->java.io.InputStream
image/jpeg-->java.net.URL
image/png-->[B
image/png-->java.io.InputStream
image/png-->java.net.URL
application/x-java-jvm-local-objectref-->java.awt.print.Pageable
application/x-java-jvm-local-objectref-->java.awt.print.Printable
application/octet-stream-->[B
application/octet-stream-->java.net.URL
application/octet-stream-->java.io.InputStream
The code snippet I am using is:
try
{
PrintService
defaultService=PrintServiceLookup.lookupDefaultPrintService();
javax.print.attribute.PrintRequestAttributeSet attributes
= new javax.print.attribute.HashPrintRequestAttributeSet();
DocFlavor flavDoc=DocFlavor.STRING.TEXT_PLAIN;
PrintService[]
services=PrintServiceLookup.lookupPrintServices(null, null);
PrintService service = ServiceUI.printDialog(null, 50, 50,
services, defaultService, null, attributes);
if (service!=null)
{
DocFlavor[] flavs=service.getSupportedDocFlavors();
if (flavs.length==0)
System.out.println("No Flavors supported!!!");
for (int i=0; i<flavs.length; i++)
{
System.out.println(flavs.getMimeType() + "-->"
+ flavs.getRepresentationClassName());
}
String toPrint=txtResults.getText();
SimpleDoc prnDoc=new SimpleDoc(toPrint, flavDoc,
null);
DocPrintJob prnJob=service.createPrintJob();
prnJob.print(prnDoc, attributes);
}
}
catch (Exception e)
{
e.printStackTrace();
javax.swing.JOptionPane.showMessageDialog(this,
e.toString());
}
When I run the program, it runs until the last command (prnJob.print)
and there it crashes. I first tried to use the flavor as a filter for
the printservices, but found that none were returned.
Does this mean I cannot print text on my printer? Is there another
representation class/Mime Type that I can use? Or is there another way
to print text files or large strings (possibly spanning many pages of
A4 sheets) in Java?
Please let me know. I have searched the internet but have not found
much to lead me...
Thanking you in advance,
Arun Kumar Srinivasan.
I am trying to use the javax.print classes to print some text
(currently as String, but can be modified to read it from a text
file). I am having trouble because it appears as if the printer (HP
LaserJet) doesnt seem to support printing text. I tried to use
DocFlavor.STRING.TEXT_PLAIN as well as
DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST but I got a flavor not
supported error. So, I took the selected printer and asked to display
its supported flavors (Mime type and representation class name) and
this is the result I got:
image/jpeg-->[B
image/jpeg-->java.io.InputStream
image/jpeg-->java.net.URL
image/png-->[B
image/png-->java.io.InputStream
image/png-->java.net.URL
application/x-java-jvm-local-objectref-->java.awt.print.Pageable
application/x-java-jvm-local-objectref-->java.awt.print.Printable
application/octet-stream-->[B
application/octet-stream-->java.net.URL
application/octet-stream-->java.io.InputStream
The code snippet I am using is:
try
{
PrintService
defaultService=PrintServiceLookup.lookupDefaultPrintService();
javax.print.attribute.PrintRequestAttributeSet attributes
= new javax.print.attribute.HashPrintRequestAttributeSet();
DocFlavor flavDoc=DocFlavor.STRING.TEXT_PLAIN;
PrintService[]
services=PrintServiceLookup.lookupPrintServices(null, null);
PrintService service = ServiceUI.printDialog(null, 50, 50,
services, defaultService, null, attributes);
if (service!=null)
{
DocFlavor[] flavs=service.getSupportedDocFlavors();
if (flavs.length==0)
System.out.println("No Flavors supported!!!");
for (int i=0; i<flavs.length; i++)
{
System.out.println(flavs.getMimeType() + "-->"
+ flavs.getRepresentationClassName());
}
String toPrint=txtResults.getText();
SimpleDoc prnDoc=new SimpleDoc(toPrint, flavDoc,
null);
DocPrintJob prnJob=service.createPrintJob();
prnJob.print(prnDoc, attributes);
}
}
catch (Exception e)
{
e.printStackTrace();
javax.swing.JOptionPane.showMessageDialog(this,
e.toString());
}
When I run the program, it runs until the last command (prnJob.print)
and there it crashes. I first tried to use the flavor as a filter for
the printservices, but found that none were returned.
Does this mean I cannot print text on my printer? Is there another
representation class/Mime Type that I can use? Or is there another way
to print text files or large strings (possibly spanning many pages of
A4 sheets) in Java?
Please let me know. I have searched the internet but have not found
much to lead me...
Thanking you in advance,
Arun Kumar Srinivasan.