Solved

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

Posted on 2008-06-12
6
6,167 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
  • 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 500 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 35
CSV file parsing thru Java 13 34
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 45
Running JavaFX on the Raspberry Pi 27 48
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

821 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