Re: Using SimpleDoc() with DocFlavour String (not InputStream)



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?
.