Link to home
Create AccountLog in
Avatar of faunus
faunus

asked on

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:
https://www.experts-exchange.com/questions/22593999/Printing-MediaTray-Options.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.

Avatar of MuhammadAdil
MuhammadAdil
Flag of Pakistan image

Avatar of faunus
faunus

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.
Avatar of faunus

ASKER

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