【问题标题】:Hiding "Print to file" in a Java print dialog在 Java 打印对话框中隐藏“打印到文件”
【发布时间】:2010-05-20 13:56:43
【问题描述】:

我正在维护这个具有“打印”选项的 Swing 应用程序。需要防止用户以任何方式与底层文件系统进行交互,但打印对话框提供“打印到文件”作为一台打印机,当然还允许从文件系统中选择目录和文件。

是否有一种轻松的方法来覆盖/修改打印对话框以从该对话框中隐藏“to file”打印机?我知道 API 可以让我零碎地完成这项工作,但我宁愿不必重新创建大部分对话框 GUI 和功能来执行此操作。

【问题讨论】:

  • 如果他们尝试打印到文件,最糟糕的情况是什么?如果您正在使用 SecurityManager 并且没有授予他们打印到文件所需的 FilePermission,那么打印对话框是否仍然允许打印到该文件?如果是这样,那就太可怕了。 :-(

标签: java security user-interface swing printing


【解决方案1】:

来自Sun论坛(http://72.5.124.102/thread.jspa?messageID=10931926#10931926),虽然您无法隐藏按钮,但看起来您可以捕获选择它的事件并引发异常:

DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; 
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

PrintService[] printerArray = dialogPrinters.toArray(new PrintService[dialogPrinters.size()]);

// call print dialog and get print attributes
PrintService selectedPrinter = javax.print.ServiceUI.printDialog(null, 200, 200, printerArray, defaultPrintService, flavor, pras);  

// check if "print-to-file" option used
if (pras.get(Destination.class) != null)
{
    // here we deny to perform the save into a file
    JOptionPane.showMessageDialog(CMSJRViewer.this, getBundleString("error.printing"));
    throw new PrintException("Print to file option not allowed. Action aborted!");
}
else
{
    ...
}

【讨论】:

  • 哇,感谢您挖掘它!但看起来对话最终会显示出来,在这种情况下,犯规行为已经完成。不过,我明天会测试一下。
【解决方案2】:

从我在抽象PrinterJob的实现类中可以看到,不,这似乎是不可能的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多