JavaFX printapi bad papersource

I am using JavaFX Print dialog to customize print jobs All properties are stored in the printerjob#jobsettings variable, but when I receive a paper source from jobsetting, the paper source is always the default

How do I get the paper source I set?

This is a short example:

public class PrinterPaperSourceTest extends Application {
    public static void main(String[] args) {
        launch( args );
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Printer");
        Button btn = new Button();
        btn.setText("Show Printer Settings ");
        btn.setOnAction( new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                PrinterJob job = PrinterJob.createPrinterJob(Printer.getDefaultPrinter());
                job.showPageSetupDialog(null);
                Alert alert = new Alert(AlertType.INFORMATION);
                PaperSource paperSource = job.getJobSettings().getPaperSource();
                alert.setContentText("PaperSource: " + paperSource.getName());
                alert.show();
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root,300,250));
        primaryStage.show();
    }
}

Solution

I have no answer, but I will try to explain why it happens and why it is not easy to repair This behavior seems to be influenced by the Internet print protocol (IPP) specification and is caused by the way the Java print service API (to which JavaFX print jobs are delegated) implements IPP The following is a snippet from the Oracle technical note that explains the limitations of manually setting the paper source( https://docs.oracle.com/javase/8/docs/technotes/guides/jps/spec/attributes.fm5.html ):

Therefore, mediatray (or paper source) is not an independent parameter. If the media property has been defined by one of the other two methods (mediasizename or medianame), it cannot be set This is exactly what happens in the page setup dialog box

The j2dprinterjob class (from the com. Sun. Prism. J2d. Print package) contains dialog code and updates print job settings (I found this by debugging your application) The following is how to update the paper source settings from the dialog box in this class

private void updatePaperSource() {
    Media m = (Media)printReqAttrSet.get(Media.class);
    if (m instanceof MediaTray) {
        PaperSource s = j2dPrinter.getPaperSource((MediaTray)m);
        if (s != null) {
            settings.setPaperSource(s);
        }
    }
}

I tested different scenarios and the results were the same: when updatepapersource() started executing, the media property had been defined as the mediasizename type So the statements in the if branch will never be executed, which is why the paper source is not updated

I suspect that the paper type or paper size takes precedence over the paper source, and because the page setup dialog always defines the paper type (there is no automatic option), it overloads the selection of the paper source to avoid property conflicts This actually makes this option useless

It may be an error in the JDK or an intentional design decision Anyway, considering that it comes from private methods in Java's internal API, I don't see a simple way to solve this problem in JavaFX

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>