Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JasperReports support alternating gutter margins yet?

Many people who generate PDFs need to bind them. A good binding requires that every other page support an alternate margin size on its left and right sides. I know JasperReports did not support this in its 3.x series. Is this supported in the 4.x series?

like image 276
AKWF Avatar asked Apr 21 '11 17:04

AKWF


2 Answers

You can accomplish marginMirroring as mentioned by Dave, by subclassing JRPdfExporter, overriding the method, exportReportToStream. Unfortunately, you will need to copy the source for this method into your override. In your override, you will modify the page loop, like so:

for(int pageIndex = startPageIndex; pageIndex <= endPageIndex; pageIndex++)
{
    int margin = marginLeft;
    if (pageIndex % 2 == 1) margin = marginRight;

    parameters.put(JRExporterParameter.OFFSET_X, margin);
    setOffset();
    ...

The constructor for my subclass takes in the margins:

public MirroringJRPdfExporter(int left, int right, int top, int bottom) {
    this.marginLeft = left;
    this.marginRight = right;
    this.marginTop = top;
    this.marginBottom = bottom;
}    

I took in top and bottom too, just in case I needed to mirror that for page flipping.

Another unfortunate note, exportReportToStream uses a helper, JRPdfExporterTagHelper, and calls 2 methods, init and setPdfWriter, which are protected, so your subclass will not compile unless you subclass the helper too and expose those methods to your subclass. I did this:

public class JRPdfExporterTagHelper extends
        net.sf.jasperreports.engine.export.JRPdfExporterTagHelper {

    protected JRPdfExporterTagHelper(JRPdfExporter exporter) {
        super(exporter);
    }

    public void setPdfWriter2(PdfWriter pdfWriter) {
        setPdfWriter(pdfWriter);
    }

    public void init2(PdfContentByte pdfContentByte) {
        init(pdfContentByte);
    }
} 

Then, I call it like this:

MirroringJRPdfExporter exporter = new MirroringJRPdfExporter(72, 36, 44, 31);

exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
exporter.exportReport();
like image 117
bigspotteddog Avatar answered Oct 12 '22 08:10

bigspotteddog


In JasperReports 6.x you can specify margins for even and odd pages separately in the report template (jrxml) by setting

<property name="net.sf.jasperreports.export.pdf.odd.page.offset.x" value="10"/>
<property name="net.sf.jasperreports.export.pdf.even.page.offset.x" value="-10"/>

An example can be found from the JasperReports sample file demo/samples/query/reports/QueryReport.jrxml. I found this solution in an issue.

The same can be set using the JRPdfExporter class when exporting the report to pdf in Java:

JRPdfExporter exporter = new JRPdfExporter();
SimplePdfReportConfiguration configuration = new SimplePdfReportConfiguration();
configuration.setOddPageOffsetX(10);
configuration.setEvenPageOffsetX(-10);
exporter.setConfiguration(configuration);
like image 41
Mikko Suonio Avatar answered Oct 12 '22 07:10

Mikko Suonio