Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using SimpleDoc() with DocFlavour String (not InputStream)

9 views
Skip to first unread message

Ian Wilson

unread,
Sep 28, 2006, 11:32:46 AM9/28/06
to
When trying to use SimpleDoc I get this error:

Exception in thread "main" java.lang.IllegalArgumentException: data is
not of declared type
at javax.print.SimpleDoc.<init>(Unknown Source)
at PrintPsString.main(PrintPsString.java:32)

No doubt because I am specifying a DocFlavour of INPUT_STREAM
(DocFlavor.INPUT_STREAM.POSTSCRIPT) and supplying a String.

Unfortunately DocFlavor.STRING doesn't include a
DocFlavor.STRING.POSTSCRIPT and using
DocFlavor.STRING.TEXT_PLAIN I get no available printers

I can make it work if I assign a FileInputStream to a file containing
the same data. What can I do to print data created dynamically in my
program?

------------------------------8<------------------------------
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

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

// data to be printed
StringBuffer sb = new StringBuffer();
sb.append("%!PS\n");
sb.append("/Helvetica 24 selectfont\n");
sb.append("100 700 moveto\n");
sb.append("(Simple Postscript) show\n");
sb.append("showpage\n");

// select target printer
DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
PrintRequestAttributeSet aset = new
HashPrintRequestAttributeSet();
PrintService[] pservices = PrintServiceLookup
.lookupPrintServices(flavor, aset);
if (pservices.length == 0) {
System.out.println("No suitable printers");
System.exit(0);
}
DocPrintJob pj = pservices[0].createPrintJob();
System.out.printf("Printing to '%s' \n",
pservices[0].getName());

// printable 'document'
String s = sb.toString();
Doc doc = new SimpleDoc(s, flavor, null);

// print it
try {
pj.print(doc, aset);
System.out.println("Your document has been Printed");
} catch (PrintException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}

}
}
------------------------------8<------------------------------

Ian Wilson

unread,
Sep 28, 2006, 12:00:48 PM9/28/06
to
Ian Wilson wrote:
> When trying to use SimpleDoc I get this error:
>
> Exception in thread "main" java.lang.IllegalArgumentException: data is
> not of declared type
> at javax.print.SimpleDoc.<init>(Unknown Source)
> at PrintPsString.main(PrintPsString.java:32)
>
> No doubt because I am specifying a DocFlavour of INPUT_STREAM
> (DocFlavor.INPUT_STREAM.POSTSCRIPT) and supplying a String.
>

I managed to make it work by converting the String to a byte array and
using a suitable DocFlavor (the BYTE_ARRAY DocFlavours are the only
other family that include .POSTSCRIPT)

StringBuffer sb;
...
sb.append("%!PS\n"); // immutable String would be bad
...
String s = sb.toString(); // as no StringBuffer.getBytes()
byte[] ba = s.getBytes();
DocFlavour flavor = DocFlavor.BYTE_ARRAY.POSTSCRIPT;
Doc doc = new SimpleDoc(ba, flavor, null);
...

Is there a way of doing this with fewer intermediary variables and type
conversions?

0 new messages