在Java打印对话框中隐藏“打印到文件”

时间:2010-05-20 13:56:44

标签: java security user-interface swing printing

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

是否有一种无痛的方式来覆盖/修改打印对话框以隐藏此对话框中的“到文件”打印机?我理解API会让我这样做,但我不必重新创建大部分对话框GUI和功能来实现这一点。

2 个答案:

答案 0 :(得分:4)

来自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
{
    ...
}

答案 1 :(得分:0)

从我在abstract PrinterJob的实现类中看到的,不,它似乎不可能。