Jasper Report: how to customize JasperViewer so that it could only export in one format?

Kico Lobo picture Kico Lobo · Apr 6, 2010 · Viewed 8.3k times · Source

our customer is loving the Jasper viewer, but we have a problem. It exports data to several different formats (PDF, Excel, CSV, HTML, etc.), but our customer only wants to export to PDF.

How can we customize the Jasper Viewer so that the only format which our users can choose to export data is PDF?

Answer

Kico Lobo picture Kico Lobo · Apr 8, 2010

I've found a solution that, in my opinion is just terrible but worked on my case.

Well: reading the source code of the JasperViewer class, I found a protected field named viewer on that class.

So, all I had to do was write a code like this:

Field jrViewerField;
            try {
                jrViewerField = viewer.getClass().getDeclaredField("viewer");

                jrViewerField.setAccessible(true);
                JRViewer jrViewer = (JRViewer) jrViewerField.get(viewer);
                List<JRSaveContributor> savers = new ArrayList<JRSaveContributor>();
                for (JRSaveContributor sc : jrViewer.getSaveContributors()) {

                        savers.add(sc);

                }

                for (JRSaveContributor sc : savers) {
                    if (! sc.getClass().getName().toLowerCase().contains("pdf")) {
                        jrViewer.removeSaveContributor(sc);
                    }
                }


            } catch (Exception ex) {
              ex.printStackTrace();
            } 

It's not a beautiful solution, but at least it worked with the 3.7.1 version of Jasper Reports. It have NO WARRANTY that may work with another versions of the system, so I highly discourage anyone to use this solution, only if that is your last resource.