Switching page orientation in ireport

2020-03-24 08:37发布

问题:

I've read there's no way to handle mixed orientations natively using iReport, however reading the documentation I wonder if by using JRDefaultScriptlet's beforePageInit() it could be accomplished somehow. In my case there's a portrait front page, as many landscape pages as there's data to populate them, and a last frontal page.

On the other hand does anybody know:

  • If this is a feature to be supported in the near future
  • If there's an alternative that does as requested and generates a jasper-compliant xml file

Thanks in advance.

回答1:

So I decided to play around with iReport and see what options there where for this. Turns out it is sort of possible to pull off, with some effort and imagination. This is assuming your first page is in the Title Section, and your Last Page is in the summary section.

  1. Create your report in landscape mode.

  2. Under Report Properties in iReport set Title on New Page and Summary on New Page to true.

  3. Assuming you are using a standard 8.5" X 11" Letter sized page with all the margins set to 20, set the height of the Title and Summary sections to 572.

  4. Add your static text fields into the appropriate section.

  5. Now for each static text field you need to set the Rotate property to Left (well it could actually be Right, the point is they all need to be the same.

  6. Of course add the all the other fields you want into the appropriate bands for page header, data, etc.

  7. Export your report.

Note: if you have any images that need to go into the Title or Summary Section your will need to rotate them appropriately outside of iReport and save it. Then set the rotated image as the image in the report. Unfortunately the image tag does not seem to have a rotate property, as that would make life to easy.

Also if you do not set the properties listed in step 2 you will not be able to set the height of the Title and Summary bands to the appropriate width. If you are using a different size paper and/or margins the easy way to figure out the max size (which is what you will need) is to set the height of the band to a very large number. It will then popup and tell you it is to large, and what the max size actually is.



回答2:

There is no support to mixed landscape and portrait subreport, in the future they will add an object call JasperBook or something like that where you can add different subreports of different orientations without problems, but for the moment you have simulate that by doing different reports and join them just before showing them.

I.E.

//Create the reports separately

        InputStream report1 = (InputStream) getClass().getResourceAsStream("/com/app/jasper/reportPortrait.jasper");
        InputStream report2 = (InputStream) getClass().getResourceAsStream("/com/app/jasper/reportLandscape.jasper");
        InputStream report3 = (InputStream) getClass().getResourceAsStream("/com/app/jasper/reportPortrait.jasper");

        JasperPrint jasperPrint = JasperFillManager.fillReport(report, map, conn);
        JasperPrint jasperPrint2 = JasperFillManager.fillReport(report2, map, conn);
        JasperPrint jasperPrint3 = JasperFillManager.fillReport(report3, map, conn);

        JRPdfExporter exp = new JRPdfExporter();

//Add the JasperPrint objects to an ArrayList

        List list = new ArrayList(); 

        list.add(jasperPrint);
        list.add( jasperPrint2 );
        list.add(jasperPrint3);

//And say to the exporter to join the list of reports.

        exp.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, list); 
        exp.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exp.exportReport();

I'm doing that in my reports and it works. Good luck!