Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to select a printer tray and paper size for printing with java

Posted on 2008-06-12
6
Medium Priority
?
6,449 Views
Last Modified: 2008-06-15
Hi,

I have a printer with different paper trays, and some trays have different paper sizes.
For example I have Tray 3, with paper size DIN/ISO A5 (5.83" x 8.27")

I want to write a program which automatically (not having shown a printer dialog) prints a document (using JasperReports) on that tray. This tray I can select with putting a Win32MediaTray class object to the PrintRequestAttributeSet as described in the following solution:
http://www.experts-exchange.com/Programming/Languages/Java/Q_22593999.html

The problem is that this document is sent with DIN/ISO A4 to the printer and the printer either shows an error or the print out is on the wrong position on the paper.
How do I also set the paper size for the print out? When I put the MediaSizeName.ISO_A5 Object to the PrintAttributeSet, the paper size is set correctly, but then not the correct tray is used any more. So it seems that putting a MediaSizeName-Object to the PrintAttributeSet overwrites the Win32MediaTray-Attribute set.

How is it possible to set both printer tray and paper size?





is it possible to select the paper tray and the paper size for printing in Java.

0
Comment
Question by:faunus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:MuhammadAdil
ID: 21768082
0
 

Author Comment

by:faunus
ID: 21768763
Hello,

thank you for your answer. I've already found this page, but unfortunatly it's not a solution.
As described, I can select a printer tray, but not togethter with the paper size.
To change the media size is also no solution, as it's to unflexible when new printers will be installed...
Any other ideas?
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 2000 total points
ID: 21770058
Perhaps you should read this :

"The value of the Media attribute is always a String, but because the attribute is overloaded,
its value determines the type of media to which the attribute refers. For example, the IPP predefined
set of attribute values include the values a4 and top-tray. If Media is set to the
value a4 then the Media attribute refers to the size of paper, but if Media is set to toptray
then the Media attribute refers to the paper source. Because the String attribute value
can refer to such diverse types of media, an application can extend the attribute set to include
values such as company-letterhead or yellow letter paper. Of course, to extend the Media
attribute in this way, an application must discover a print service that is configured to print
with this media."

from:

http://java.sun.com/j2se/1.4.2/pdf/JPS_PDF.pdf

(If I understood your question correctly).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 2000 total points
ID: 21770100
... actually, I meant to post this quote more appropriately :

"In most cases, applications will use either MediaSizeName or MediaTray. The MediaSizeName
class enumerates the media by size. The MediaTray class enumerates the paper trays on a
printer, which usually include a main tray and a manual feed tray. The IPP 1.1 specification
does not provide for specifying both the media size and the media tray at the same time,
which means, for example, that an application cannot request size A4 paper from the manual
tray. A future revision of the IPP specification might provide for a way to request more than
one type of media at a time, in which case the JPS API will most likely be enhanced to
implement this change."
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 21776818
Your workaround might then seem to be the construction of your own (perhaps constructor-overloaded)  print class which can mitigate but not fully circumvent, the compromises and inflexibilities of this situation should it be the case.
0
 

Author Comment

by:faunus
ID: 21787837
I've found a solution by myself, debugging the PrintRequestAttributeSet.

As I already supposed in my question, setting MediaSizeName in the PrintRequestAttributeSet overrides a former setting of MediaTray and vice versa. This is because both classes are subclasses from Media and return the same category. (only one entry per category is allowed in PrintRequestAttributeSet).
If I put a SunAlternateMedia-Object to the PrintRequestAttributeSet (which returns a different category), I can additionaly put a MediaSizeByName object to the PrintRequestAttributeSet.
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
Media med[] = (Media[])service.getSupportedAttributeValues(Media.class, null, null);
for (int k=0; k<med.length; k++) {
  if(med[k].getClass().equals(sun.print.Win32MediaTray.class) &&
                    med[k].toString().trim().contains("Tray 3")) {
    SunAlternateMedia am = newSunAlternateMedia(med[k]);
    pras.add(am);  // Adding the value for the paper tray
    pras.add(MediaSizeName.ISO_A5); //adding the value for page size
}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

618 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