Selecting MediaTray in Java printing

Posted by Rocket Surgeon on Stack Overflow See other posts from Stack Overflow or by Rocket Surgeon
Published on 2010-05-11T22:11:57Z Indexed on 2010/05/11 22:14 UTC
Read the original article Hit count: 362

Filed under:
|

I am trying to programmatically select a different media tray using Java Printing API. However, my document always gets printed to the default (TOP) media tray. I checked if the MediaTray attributes are supported using "isAttributeValueSupported()" method on javax.print.PrintService interface and I am getting the result as "true" for each MediaTray I pass. Here is my code:

public void print(
        String printerName,
        com.company.services.document.transferobject.MediaTray tray,
        byte[] document) {
    String methodName = "print: ";
    logger.sendEvent(CLASS_NAME + methodName + "Start", EventType.INFO,
            this);


    if (printerName == null || "none".equals(printerName)
            || "?".equals(printerName) || "null".equals(printerName)) {
        logger.sendEvent("Please supply printer name, currently printerName is "+printerName,
                EventType.INFO, this);
        return;
    }

    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    AttributeSet attributeSet = new HashAttributeSet();
    attributeSet.add(new PrinterName(printerName, null));

    javax.print.PrintService service = getService(printerName);

    if (service.isAttributeValueSupported(MediaTray.TOP, flavor, null)) {
        System.out.println("---------->>>>>>>>>Yes TOP" + " : Value : "
                + MediaTray.TOP.getValue());
    } else {
        System.out.println("---------->>>>>>>>>Nope");
    }

    if (service.isAttributeValueSupported(MediaTray.BOTTOM, flavor, null)) {
        System.out.println("---------->>>>>>>>>Yes BOTTOM" + " : Value : "
                + MediaTray.BOTTOM.getValue());
    } else {
        System.out.println("---------->>>>>>>>>Nope");
    }

    if (service.isAttributeValueSupported(MediaTray.MIDDLE, flavor, null)) {
        System.out.println("---------->>>>>>>>>Yes MIDDLE" + " : Value : "
                + MediaTray.MIDDLE.getValue());
    } else {
        System.out.println("---------->>>>>>>>>Nope");
    }

    if (service.isAttributeValueSupported(MediaTray.MANUAL, flavor, null)) {
        System.out.println("---------->>>>>>>>>Yes MANUAL" + " : Value : "
                + MediaTray.MANUAL.getValue());
    } else {
        System.out.println("---------->>>>>>>>>Nope");
    }

    if (service.isAttributeValueSupported(MediaTray.SIDE, flavor, null)) {
        System.out.println("---------->>>>>>>>>Yes SIDE" + " : Value : "
                + MediaTray.SIDE.getValue());
    } else {
        System.out.println("---------->>>>>>>>>Nope");
    }

    DocPrintJob printJob = service.createPrintJob();
    try {
        byte[] textStream = document;
        PrintRequestAttributeSet pras =
                new HashPrintRequestAttributeSet();
        pras.add(DocumentServiceConstant.
                    DEFAULT_ONE_PRINT_COPY);
        pras.add(Sides.ONE_SIDED);
        Media standardTray= toStandardTray(tray);
        if (null != standardTray) {
            pras.add(standardTray);
        }

        Doc myDoc = new SimpleDoc(textStream, flavor, null);
        printJob.print(myDoc, pras);
        logger.sendEvent(
                " successfully printed ............ ",
                EventType.INFO, this);
    } catch (Throwable th) {
        logger.sendEvent(" Throwable : "+th.getLocalizedMessage(),
                        EventType.INFO, this);
        ExceptionUtility
                .determineExceptionForServiceClient(th);
    }

    logger.sendEvent(CLASS_NAME + methodName + "END: ", EventType.INFO,
            this);

}

Any help will be greatly appreciated!

© Stack Overflow or respective owner

Related posts about java

Related posts about printing