Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6553
  • Last Modified:

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

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
faunus
Asked:
faunus
  • 3
  • 2
2 Solutions
 
MuhammadAdilCommented:
0
 
faunusAuthor Commented:
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
 
krakatoaCommented:
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!

 
krakatoaCommented:
... 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
 
krakatoaCommented:
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
 
faunusAuthor Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now