Retrieving Printerjob Attributes

I am using java awt PrinterJob to show a Print Dialog and submit a job to print. I would like to read the attributes selected by the user from the dialog before the job is submitted, but I see that 'attributes' is a protected variable, so not sure how I can access it. I need to run some analysis on the print job being submitted. Is there a workaround here?

2

1 Answer

Instead of method PrinterJob.printDialog() you should use method PrinterJob.printDialog(PrintRequestAttributeSet). In the supplied PrintRequestAttributeSet you will receive all the attributes as chosen by the user in the print dialog.

From this PrintRequestAttributeSet you can then

  • get all the attributes by calling its toArray() method
  • or get individual attributes by using the get(Class<?>) method
    (use the classes implementing PrintRequestAttribute from package javax.print.attribute.standard as keys.)

Then you can change the PrintRequestAttributeSet by removing or adding attributes.

And finally you submit the PrinterJob by calling its print() method.

Example:

import java.awt.print.PrinterJob;
import javax.print.attribute.Attribute;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.*;

public static void main(String[] args) throws Exception {
    PrinterJob job = PrinterJob.getPrinterJob();
    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    boolean ok = job.printDialog(attributes);

    Attribute[] attributeArray = attributes.toArray();
    for (Attribute a : attributeArray) {
        System.out.println(a.getName() + ": " + a);
    }

    System.out.println();

    Attribute copies = attributes.get(Copies.class);
    Attribute media = attributes.get(Media.class);
    Attribute mediaPrintableArea = attributes.get(MediaPrintableArea.class);
    Attribute mediaTray = attributes.get(MediaTray.class);
    Attribute orientationRequested = attributes.get(OrientationRequested.class);
    Attribute sides = attributes.get(Sides.class);
    System.out.println("copies: " + copies);
    System.out.println("media: " + media);
    System.out.println("mediaPrintableArea: " + mediaPrintableArea);
    System.out.println("mediaTray: " + mediaTray);
    System.out.println("orientationRequested: " + orientationRequested);
    System.out.println("sides: " + sides);

    attributes.remove(Sides.class);
    attributes.add(Sides.DUPLEX);

    job.print();
}

With the example above I got this output:

media: iso-a4
orientation-requested: portrait
media-printable-area: (25.4,25.4)->(159.2,246.2)mm
copies: 1

copies: 1
media: iso-a4
mediaPrintableArea: (25.4,25.4)->(159.2,246.2)mm
mediaTray: null
orientationRequested: portrait
sides: null
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest