Solved

Printer-questions

Posted on 2004-10-27
339 Views
Last Modified: 2008-01-09
How can I get/set the paper tray of printers? My intentions are to allow people to select a printer from inside a java-applet & let them set the printer and the papertray and store those in a server-sided database.
Then when they want to print something I want to programmaticaly set the bin/printer and print on that one.

So what I need (in order of importance):
- How to get/set paper format (-)
- How to get/set the printer name (+)
- How to get/set the paper tray (++)

Thanks
0
Question by:half_life_fool
    12 Comments
     
    LVL 12

    Expert Comment

    by:Giant2
    See here:
    http://www.javaalmanac.com/cgi-bin/search/find.pl?words=Print
    There are some example to print settings.
    About the insert into DB you can use a normal JDBC (I believe you know how to use it).

    Bye, Giant.
    0
     
    LVL 4

    Expert Comment

    by:lcwiding
    First, you will need to use the Properties class to set the properties you want. In order to determine the correct values, that will depend on the printer.

    But, perhaps more importantly, this does not work on most Java releases.
    0
     

    Author Comment

    by:half_life_fool
    Is it possible to give some examples? Because I am totally puzzled here. I am rather good at C++, but this Java thing eludes me :(
    0
     
    LVL 4

    Expert Comment

    by:lcwiding
    You would start the prinjob with:

    Properties prop = new Properties();
    Toolkit tk = getToolkit();
    PrintJob pj = tk.getPrintJob(frame, "job title", prop);

    This will work in all JRE releases (Microsoft, Sun, ...);

    For Sun JRE 1.3 and later, you can use:

    JobAttributes ja = new JobAttributes();
    ja.setDialog(JobAttributes.DialogType.NONE);
    ja.setPrinter("printer name");
    PageAttributes pa = new PageAttributes();
    PrintJob = tk.setPrintJob(frame, "job title", ja, pa);

    I did not find a way to set the tray using these, and how well they work still depends on the printer in the user's system, and whether or not it supports these functions. I have not used the second form mysefl, because the needs of my applications have required suport for 1.1, and have not had need for the level of support granted by the newer method.

    For more informatio on these, see:
    http://java.sun.com/j2se/1.3/docs/api/java/awt/JobAttributes.html
    http://java.sun.com/j2se/1.3/docs/api/java/awt/PageAttributes.html
    0
     

    Author Comment

    by:half_life_fool
           JobAttributes theJobAttribs = new JobAttributes();
            theJobAttribs.setDialog( JobAttributes.DialogType.NONE );
            theJobAttribs.setPrinter("PDFCreator");
           
            PageAttributes thePageAttribs = new PageAttributes();
            thePageAttribs.setMedia( PageAttributes.MediaType.LETTER );
    //        thePageAttribs.setMedia( MediaTray.MAIN );
           
            PrintJob myJob = getToolkit().getPrintJob(this, "PrintJob", theJobAttribs, thePageAttribs);
            myJob.setAttribute();
            if (myJob != null) {
                Graphics g = myJob.getGraphics();
               
                if (g != null) {
                    printText(myJob, g, "yeqsd");
                    g.dispose();
                }
               
                myJob.end();
            }


    This however gives an error on getToolkit() & on printText()

    Also there SHOULD be a possibility to get/set the MediaTray.

    http://java.sun.com/j2se/1.5.0/docs/api/javax/print/attribute/standard/MediaTray.html

    0
     
    LVL 4

    Expert Comment

    by:lcwiding
    What errors are generated by these calls?

    getToolkit() is defined in java.awt.Component, so it shuold be callable from within any class that extends that.

    I have not used the javax.print classes, but yes, it does look like you could use DocPrintJob to add the additional support you mention. The only caveat is that I am not sure with JRE that was implemented in, or if that is a concern for you.
    0
     

    Author Comment

    by:half_life_fool
    D:\Documents and Settings\Steven\JavaApplication2\src\javaapplication2\Main.java:40: cannot find symbol
    symbol  : method getPrintJob(javaapplication2.Main,java.lang.String,java.awt.JobAttributes,java.awt.PageAttributes)
    location: class java.awt.Toolkit
            PrintJob myJob = getToolkit().getPrintJob(this, "PrintJob", theJobAttribs, thePageAttribs);
    D:\Documents and Settings\Steven\JavaApplication2\src\javaapplication2\Main.java:45: cannot find symbol
    symbol  : method printText(java.awt.PrintJob,java.awt.Graphics,java.lang.String)
    location: class javaapplication2.Main
                    printText(myJob, g, "yeqsd");
    2 errors
    0
     
    LVL 4

    Expert Comment

    by:lcwiding
    What version of Java are you running this under? The revised getPrintJob method was first made available with Java 1.3. If you are using an earlier release, you need to stick with my original recommendation. In that case, I would recommend that you start the print job, using the Print Dialgo to set the options that you want, and then, when getPrintJob() returns, dump the contents of the Properties object to the Java console, so you can preload those settings.

    As for the printText method, I cannot speak to that, as I do not know that method. Is that one you wrote? If not, what class is it from?
    0
     

    Author Comment

    by:half_life_fool
    I'm running it under java 1.5.0.

    I am coding in Netbeans 4.0 beta.

    BTW, how do I dump that object to the Java console? Because when I set a watch in NB, it doesn't provide me with much usefull information...

    Thx!
    0
     
    LVL 4

    Accepted Solution

    by:
    A simple way to output the properies object would be as follows:

    properties.list(System.out);
    0
     
    LVL 4

    Expert Comment

    by:lcwiding
    I looked at the code you said was reporting errors. When I brought it up in Eclispe with the JDK 1.5 libraries, I got an error that setAttribute() was not a member of PrintJob, and could not find printText, which, as I said, looks like one of your methods.
    0
     
    LVL 4

    Expert Comment

    by:lcwiding
    I do not know if the author ever found a solution, though I tried to direct him as much as I could based on the information provided. My recommendation would be to mark my comment from 10/31/2004 05:26AM CST as accepted.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now