开发者

Does JasperReports support alternating gutter margins yet?

Man开发者_运维技巧y 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?


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();


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);


To work with jasper 5.6 besides the answer to @bigspotteddog I did:

@Override
protected PdfReportConfiguration getCurrentItemConfiguration() {
    SimplePdfReportConfiguration config = new SimplePdfReportConfiguration();
    PdfReportConfiguration currentItemConfiguration = super.getCurrentItemConfiguration();
    config.setCollapseMissingBookmarkLevels(currentItemConfiguration.isCollapseMissingBookmarkLevels());
    config.setForceLineBreakPolicy(currentItemConfiguration.isForceLineBreakPolicy());
    config.setForceSvgShapes(currentItemConfiguration.isForceSvgShapes());
    config.setIgnoreHyperlink(currentItemConfiguration.isIgnoreHyperlink());
    config.setOverrideHints(currentItemConfiguration.isOverrideHints());
    config.setSizePageToContent(currentItemConfiguration.isSizePageToContent());
    config.setEndPageIndex(currentItemConfiguration.getEndPageIndex());
    config.setExporterFilter(currentItemConfiguration.getExporterFilter());
    config.setHyperlinkProducerFactory(currentItemConfiguration.getHyperlinkProducerFactory());
    config.setPageIndex(currentItemConfiguration.getPageIndex());
    config.setProgressMonitor(currentItemConfiguration.getProgressMonitor());
    config.setStartPageIndex(currentItemConfiguration.getStartPageIndex());

    config.setOffsetX(margin);

    return config;
}

and :

margin = marginLeft;
if (pageIndex % 2 == 1) margin = marginRight;

in the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜