Hiding "Print to file" in a Java print dialog
java, printing, security, swing, user-interface
Solution
From the Sun forums (http://72.5.124.102/thread.jspa?messageID=10931926#10931926), while you cannot hide the button, it looks like you can capture the event of selecting it and throw an exception:
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
{
...
}
Problem
I'm maintaining this Swing app that has a "print" option. Users need to be kept from interacting in any way with the underlying file system, but the print dialog offers "print to file" as one printer, and that of course allows selecting a directory and file from the file system. Is there a painless way to override/modify the print dialog to hide the "to file" printer from this dialog? I understand the API will let me do this piecemeal but I'd rather not have to re-create most of the dialog GUI and functionality to do this.