Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8134271

Programatically selecting paper size creates IllegalArgumentSelection

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • tbd
    • 8
    • javafx
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Printers:
      Brother HL-2270DW (Default printer)
      Brother MFC-6490CW
      CutePDF Writer

      A DESCRIPTION OF THE PROBLEM :
      I am using JavaFX 8 with JDK 1.8u45. In my application, I let the user configure defaults for paper size and orientation for print jobs of a JavaFX component. However, during the print job, the user could modify the selected paper size and orientation using the system print dialogs, called from JavaFX. In the end, I take into account the final PageLayout of the print job to determine the paper size, number of page, node transformations, etc. to perform the actual print.

      My code is fairly simple. For the sake of simplicity, I will not use user preference, and will assume the user preference for paper size/orientation is Tabloid/Landscape.

      I use this simple chunk of code to setup the print job:

      PrinterJob job = PrinterJob.createPrinterJob(
      job.getJobSettings().setPageLayout(
              job.getPrinter().createPageLayout(
                      Paper.TABLOID,
                      PageOrientation.LANDSCAPE,
                      Printer.MarginType.DEFAULT));
      boolean doPrint = job.showPageSetupDialog(getScene().getWindow());
      if (doPrint) {
          doPrint = job.showPrintDialog(getScene().getWindow());
      }
      if (doPrint) {
          // Perform print job here.
      }
      This looks pretty good at first, when displaying the page setup dialog. The quivalent of Tabloid, Ledger, is displayed as the selected paper size. The orientation is also good. After pressing OK, the print dialog is displayed, allowing the user to select a printer. This is where things get funky.

      A couple of prerequisites are necessary to reproduce the bug I see, but I have been able to reproduce it on 2 different computers. If any of those 3 conditions is not met, the bug will not occur and everything will work properly.

      the default printer does not support the selected paper size (TABLOID);
      the printer selected in the dialog must not be the default printer.
      The printer "Properties..." button is not pressed by the user
      Then, when the user presses the OK button, this exception is thrown:

      Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Margins must be >= 0
          at javafx.print.Printer.createPageLayout(Printer.java:346)
          at com.sun.prism.j2d.print.J2DPrinterJob.updatePageLayout(J2DPrinterJob.java:396)
          at com.sun.prism.j2d.print.J2DPrinterJob.updateSettingsFromDialog(J2DPrinterJob.java:476)
          at com.sun.prism.j2d.print.J2DPrinterJob.showPrintDialog(J2DPrinterJob.java:133)
          at javafx.print.PrinterJob.showPrintDialog(PrinterJob.java:290)
          ...
      From what I see, this appear to be a bug in the JDK, since this occurs because in J2DPrinterJob.java, the media returned by

      Media media = (Media)printReqAttrSet.get(Media.class);
      is not a valid media, labeled "b" instead of "Tabloid" or "Ledger". This causes the calculated margin to be negative, and this exception to be thrown.


      REPRODUCIBILITY :
      This bug can be reproduced always.

            prr Philip Race
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: