I have a program which has a menu item that triggers

    PageFormat pf = printerJob.pageDialog(printRequestAttributes);
    if (pf != null) {
        pageFormat = pf; // user selected OK
        // more code
   
    syncPrintRequestAttributes();
        // more code
    }


Before all this I setup printRequestAttributes with the following code. Basically when I load an image with an aspect ratio that requires a portrait orientation and trigger the pageDialog the page margins are all 1 inch by default - what I would expect. If I then select landscape, the margins stay at 1 inch in the dialog.

However, if I load an image that requires a landscape orientation the margins start out at left=3.5, right=1.0, top=0.25, bottom=0.25. If I toggle portrait in the dialog I then get left=0.25, right=0.25, top=1.0, bottom=3.5.

Going back to loading a portrait image, if I select landscape, then click the OK button in the dialog, I now get the same margin values and behavior as the paragraph above. Can anyone explain what is going on here?

I think for some reason I am unable to set in printRequestAttributes the physical size of the page. I tried that with the MediaName attribute, but this has no measurements in it that I can borrow from the known size in the pageFormat setting.

As an aside, the accuracy gets skewed because the class MediaPrintableArea is implemented in int microns and the rest of Graphic2D is implemented in int 1/72 inches.

    // This utility sets the printer job for quality printing...

    void setPrinterInfo(PrinterJob printerJob) {

        PrintService[] ps = printerJob.lookupPrintServices();

        for (int i = 0; i < ps.length; i++) {
            PrintService printService = ps[i];
            System.out.println("\nSetting Attributes for `" +
            printService.getName() + "'");

            //  Set the page orientation depending on the aspect
            //  ratio of the original image.

            if (bImage.getHeight() > bImage.getWidth()) {
                pageFormat.setOrientation(PageFormat.PORTRAIT);
            } else {
                pageFormat.setOrientation(PageFormat.LANDSCAPE);
            }

            // Synchronize the printRequestAttributes with
            // the pageFormat settings

            syncPrintRequestAttributes();

            // The remaining printRequestAttributes are outside
            // the pageFormat settings capabilities
       
            printRequestAttributes.add(MediaName.NA_LETTER_WHITE);
            printRequestAttributes.add(PrintQuality.HIGH);
         }
    }

    // This utility synchronizes the printRequestAttributes with
    // the pageFormat settings.
   
    void syncPrintRequestAttributes() {

        printRequestAttributes.add(new MediaPrintableArea(
            (float)(pageFormat.getImageableX()      / 72.0),
            (float)(pageFormat.getImageableY()      / 72.0),
            (float)(pageFormat.getImageableWidth()  / 72.0),
            (float)(pageFormat.getImageableHeight() / 72.0),
            MediaPrintableArea.INCH));

        if (pageFormat.getOrientation() == PageFormat.PORTRAIT)
            printRequestAttributes.add(OrientationRequested.PORTRAIT);
        else
            printRequestAttributes.add(OrientationRequested.LANDSCAPE);
    }

Reply via email to