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

When the native print dialog is used to select custom paper, the paper used may be different from paper selected

XMLWordPrintable

    • 2d
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
      Java HotSpot(TM) Client VM (build 25.66-b18, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When printing through java's native print dialog and more than one paper exists that have the same paper size but different draw-able areas, the incorrect paper size may be selected.

      This seems to be the case when papers that are custom or added by a printer driver (e.g. a plotter may add large scale paper sizes or multiple papers with different internal configurations).

      This is reproducible reliably under these circumstances.

      This issue looks to occur in CustomMediaSizeName.findMedia
      The paper size is selected based on it's match to the input height and width. This leads to a paper that is correct dimensions, but it will ignore any other paper sizes that match.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create custom paper size in OS (For Windows, open Printers, select Printer and click Print Server Properties along top bar)

      For this I created a copy of A4 paper, called 'A4_Margin' with 2 inch margins on all sides and set it as default on printer

      Printer used is CutePDF Creator, but problem exists on other printers as well (physical and virtual)

      Create new PrintRequestAttributeSet

      add DialogTypeSelection.NATIVE to attribute set
      add OrientationRequested.PORTRAIT to attribute set

      Open native print dialog (using attribute set) in java application, select printer, then Properties

      Within the Printer properties, select custom paper size

      Select OK . OK.

      Print using attribute set

      View the media name in the attribute set. Note that it is different from the selected paper

      View printout, note that size is correct, but the margins were not used.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The paper used to print out would be the same as the paper selected in the dialog
      ACTUAL -
      The paper used to print out was different that the paper selected in the dialog

      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      import javax.print.attribute.Attribute;
      import javax.print.attribute.HashPrintRequestAttributeSet;
      import javax.print.attribute.PrintRequestAttributeSet;
      import javax.print.attribute.standard.DialogTypeSelection;
      import javax.print.attribute.standard.Media;
      import javax.print.attribute.standard.OrientationRequested;
      import java.awt.print.PageFormat;
      import java.awt.print.PrinterException;
      import java.awt.print.PrinterJob;

      public class PrintingTestIssue1 {
          
          private static void showPrintDialog() {
              PrinterJob pJob = PrinterJob.getPrinterJob();
              PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
              pras.add(DialogTypeSelection.NATIVE);
              pras.add(OrientationRequested.PORTRAIT);

              if (pJob.printDialog(pras)) {
                  PageFormat pFormat = pJob.getPageFormat(pras);
                  pJob.validatePage(pFormat);
                  pJob.setPrintable((graphics, pageFormat, pageIndex) -> 1, pFormat);
                  try {
                      pJob.print(pras);
                  } catch (PrinterException e) {
                      e.printStackTrace();
                  }
                  reportUsedPaperSize(pras);
              }
          }

          private static void reportUsedPaperSize(PrintRequestAttributeSet pras) {
              Attribute mediaAttr = (pras != null && !pras.isEmpty()) ? pras.get(Media.class) : null;
              Media m = (mediaAttr instanceof Media) ? (Media) mediaAttr : null;
              String setMediaName = m != null ? m.toString() : null;
              System.out.println(setMediaName != null ? "Media Used :" + setMediaName : "Media Used : Unknown");
          }
          
          private static void issue() {
              System.out.println("Media selected :" + "A4_Margin");
              showPrintDialog();
              System.out.println();
          }

          public static void main(String[] args) throws PrinterException {
              // In this issue, the paper size is selected based on it's match to the input height and width. This leads
              // to a paper that is correct dimensions, but it will ignore any other paper sizes that match. It becomes an
              // issue if there are 2 papers that are the same size but have different margins.
              issue();
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Set custom paper size as default paper size on printer using Printer Preferences

      Call PrinterJob.print(PrintRequestAttributeSet) with unpopulated attribute set.

      Clear PrintRequestAttributeSet after printing

            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: