代码之家  ›  专栏  ›  技术社区  ›  Carl Smotricz

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

  •  4
  • Carl Smotricz  · 技术社区  · 14 年前

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

    有没有一种无痛的方法可以覆盖/修改打印对话框,以便在此对话框中隐藏“到文件”打印机?我知道API会让我一点一点地完成这项工作,但我不想重新创建大部分对话GUI和功能来完成这项工作。

    2 回复  |  直到 14 年前
        1
  •  4
  •   Matthew Flynn    14 年前

    来自太阳论坛 (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
  •  0
  •   Riduidel    14 年前

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