Using SimpleDoc() with DocFlavour String (not InputStream)



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

.



Relevant Pages

  • Re: Windows Default Printer not being Reset
    ... When I simply fill a listBox with all the available printers in the Printers ... Private Const HWND_BROADCAST = &HFFFF ... dmDeviceName As String * CCHDEVICENAME ... Dim sTemp As String * 512, ...
    (microsoft.public.vb.general.discussion)
  • Re: List of printers
    ... ByVal lpKeyName As String, ByVal lpDefault As String, _ ... MsgBox Join,, "List of printers" ... Dim n%, lRet&, sBuf$, sCon$, aPrn$ ... 'returns a zerobased array of complete localized printer strings ...
    (microsoft.public.excel.programming)
  • The report generated in the web server is printed out on the network shared printer
    ... There are some local printers and network shared printers installed in my ... Private strReturn As String ... As String, ByVal sChoiceUser As String, ByVal sDate As String, ByVal sTime ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: VBA ActivePrinter
    ... array of installed/available printers. ... Dim strActPrint As String ... >> returned array in a listbox or combo. ... >> Public Function ListPrinter() As Variant ...
    (microsoft.public.excel.programming)
  • Re: printing using IP address
    ... Following is not tested for IP address printers' but seems to work ... ByVal lpKeyName As String, ByVal lpDefault As String, _ ... Dim v As Variant ... lRet = GetProfileString(sKey, vbNullString, vbNullString, sBuf, lSize) ...
    (microsoft.public.excel.programming)