Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Using SimpleDoc() with DocFlavour String (not InputStream)

Thread view: 
Ian Wilson - 28 Sep 2006 16:32 GMT
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 - 28 Sep 2006 17:00 GMT
> When trying to use SimpleDoc I get this error:
>
[quoted text clipped - 5 lines]
> 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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.