我正在使用 JavaFx Print-Dialog来自定义打印作业.所有属性都将存储在PrinterJob#JobSettings变量中,但是当我从jobSetting接收纸张来源时,纸张来源始终是默认值.

如何获得我设置的纸张来源?

这是一个简短的例子:

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();
    }
}

解决方法

我没有答案,但我会尝试解释它为什么会发生以及为什么它不容易修复.此行为似乎受Internet打印协议(IPP)规范的影响,并且是由Java Print Service API(JavaFX打印作业委托给它)实现IPP的方式引起的.以下是Oracle技术说明中的一个片段,解释了手动设置纸张来源的限制( https://docs.oracle.com/javase/8/docs/technotes/guides/jps/spec/attributes.fm5.html):

Media is the IPP attribute that identifies the medium on which to print. The Media attribute is an important attribute to understand,but is relatively complex.

The Java Print Service API defines three subclasses of the abstract class Media to reflect the overloaded Media attribute in the IPP specification: MediaSizeName,MediaName and MediaTray. All the Media subclasses have the Media category,for which each subclass defines different standard attribute values. […]

The value of the Media attribute is always a String,but because the attribute is overloaded,its value determines the type of media to which the attribute refers. For example,the IPP pre-defined set of attribute values include the values “a4” and “top-tray”. If Media is set to the value “a4” then the Media attribute refers to the size of paper,but if Media is set to “top-tray” then the Media attribute refers to the paper source. […]

In most cases,applications will use either MediaSizeName or MediaTray. The MediaSizeName class enumerates the media by size. The MediaTray class enumerates the paper trays on a printer,which usually include a main tray and a manual Feed tray. The IPP 1.1 specification does not provide for specifying both the media size and the media tray at the same time,which means,for example,that an application cannot request size A4 paper from the manual tray. A future revision of the IPP specification might provide for a way to request more than one type of media at a time,in which case the JPS API will most likely be enhanced to implement this change.

因此,MediaTray(或纸张来源)不是独立参数,如果Media属性已经由其他两种方式之一(MediaSizeName或MediaName)定义,则无法设置.这正是页面设置对话框所发生的情况.

J2DPrinterJob类(来自com.sun.prism.j2d.print包)包含对话框代码并更新打印作业设置(我通过调试您的应用程序找到了这个).以下是此类中从对话框更新纸张来源设置的方法.

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);
        }
    }
}

我测试了不同的场景,结果是一样的:当updatePaperSource()开始执行时,Media属性已经被定义为MediaSizeName类型.所以if分支中的语句永远不会被执行,这就是为什么纸质来源没有更新的原因.

我怀疑纸张类型或纸张尺寸优先于纸张来源,并且因为页面设置对话框始终定义纸张类型(没有“自动”选项),它会使纸张来源的选择超载,以避免属性冲突.这实际上使这个选项无用.

它可能是JDK中的错误或有意的设计决策.无论如何,考虑到它来自Java内部API中的私有方法,我没有看到解决JavaFX中这个问题的简单方法.

JavaFX PrintAPI错误的PaperSource的更多相关文章

  1. php – wkhtmltopdf:什么纸张大小有效?

    我正在使用wkhtmltopdf,并通过PHPshell_exec来运行它.当运行wkhtmltopdf–help一个选项是大小,帮助文本是Setpapersizeto:A4,Letter,etc.我想知道有没有人可以运行这个可执行文件的纸张大小的列表?

  2. c# – 设置纸张尺寸

    请帮我说说如何用c#代码设置我的纸张尺寸.我正在使用APIprintDocument.以下是我的代码:解决方法这是一种通过类似’A4’设置纸张尺寸的方法这是另一种设置自定义纸张尺寸的方法希望它有效.

  3. JavaFX PrintAPI错误的PaperSource

    我正在使用JavaFxPrint-Dialog来自定义打印作业.所有属性都将存储在PrinterJob#JobSettings变量中,但是当我从jobSetting接收纸张来源时,纸张来源始终是默认值.如何获得我设置的纸张来源?

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部