views:

506

answers:

1

Hi,

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?

+1  A: 

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.

Kico Lobo