Q: How can I tell the printer to use a different media tray, and so a different sized paper according to the PDF?

A: Media tray selection in Java is very tricky and does not work as documented. In theory, by setting the media size, Java should get the printer to choose the correct tray. Sometimes this does not work.

If you know what paper sizes are in what trays, or if you can show the user a list of trays to pick from, then this can be made to work using the MediaTray print request attribute.

Even then, the pre-defined values in MediaTray (TOP, BOTTOM, etc) do not work. You have to list the trays and then choose one of the instances returned by the list.

The code to get the media tray list is as follows:

// Get supported media
Media [] media = (Media [])pServices[0].getSupportedAttributeValues(Media.class, null, null);
for (int count = 0; count < media.length; ++count)
if (media [count] instanceof MediaTray)
System.out.println (media [count].getValue() + ": "+ media [count].toString());

Please note a couple of things:

– When we get the attribute values, we do it on the Media class, not the MediaTray class.

– When we loop through the Media attributes returned, we only look at the MediaTray types.

– When we echo the name of the tray, we use the toString() method.getName() does not return the correct name.

By using this code, you can save either the value or the name in your user’s preferences and then search through the attributes again when the user wants to print. Once you find the matching tray, you would then set your attribute in your PrintRequestAttributeSet as:

attrSet.add (media [foundIndex]);