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

PageLayout API of Printer loses precision for margin

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • jfx13, jfx17
    • javafx
    • x86_64
    • windows_10

      Context :
      When showing a PageSetupDialog, the margin can be entered in mm by the end user. However, the API of the PageLayout only offers to retrieve them in points. During the process, the original value is manipulated several times and precision is lost.

      Steps to reproduce:
      Run this sample application :
      "
      import java.io.IOException;
      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.print.PageLayout;
      import javafx.print.PageOrientation;
      import javafx.print.Paper;
      import javafx.print.Printer;
      import javafx.print.PrinterJob;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class Main extends Application {

          public static void main(String[] args) {
              Application.launch(Main.class, args);
          }
          Double marginleft = 10.0;
          Double marginRight= 10.0;
          Double marginTop= 10.0;
          Double marginBottom= 10.0;

          @Override
          public void start(Stage stage) throws IOException {

              Button button = new Button("test");
              button.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent event) {
                      Printer printer = Printer.getDefaultPrinter();
                      PrinterJob job = PrinterJob.createPrinterJob(printer);

                      PageLayout layout = printer.createPageLayout(Paper.A4,
                              PageOrientation.PORTRAIT,
                              marginleft,
                              marginRight,
                              marginTop,
                              marginBottom);
                      job.getJobSettings().setPageLayout(layout);
                      job.showPageSetupDialog(stage);
                      marginleft = job.getJobSettings().getPageLayout().getLeftMargin();
                      marginRight = job.getJobSettings().getPageLayout().getRightMargin();
                      marginTop = job.getJobSettings().getPageLayout().getTopMargin();
                      marginBottom = job.getJobSettings().getPageLayout().getBottomMargin();
                  }
              });
              stage.setScene(new Scene(new VBox(button)));
              stage.show();
          }
      }
      "
      - Click on the test button
      - Type 10 (mm) in the dialog (see image-2019-10-31-15-14-14-695.png)
      - Click OK and re-click on the test button

      Expected result:
      Since value are retrieved and saved from the PageLayout, we should have 10mm in the dialog again.

      Actual result:
      Due to multiple conversion (value converted from micromillimeter to Inch in MediaPrintableArea then multiplied by 72 in J2DPrinterJob etc), a loss of precision happens and the value returned is wrong. See image-2019-10-31-15-14-27-172.png


      Please note that my computer is in French so my dialog is showing in french with millimeters. Reproduced on JDK8u202 and OpenJFX 13

            prr Philip Race
            shadzic Samir Hadzic
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: