Solved

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

Posted on 2008-06-12
6
6,026 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 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

10 Experts available now in Live!

Get 1:1 Help Now