JasperReports是否支持交替的天沟边缘?

时间:2011-04-21 17:03:36

标签: jasper-reports

许多生成PDF的人都需要绑定它们。良好的绑定要求每个其他页面在其左侧和右侧支持备用边距大小。我知道JasperReports在其3.x系列中并不支持这一点。这是否支持4.x系列?

3 个答案:

答案 0 :(得分:4)

您可以通过继承JRPdfExporter,覆盖方法exportReportToStream来完成Dave所提到的marginMirroring。不幸的是,您需要将此方法的源复制到您的覆盖中。在覆盖中,您将修改页面循环,如下所示:

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

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

我的子类的构造函数包含边距:

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

我也接受了顶部和底部,以防我需要镜像页面翻转。

另一个不幸的注意事项是,exportReportToStream使用一个帮助程序JRPdfExporterTagHelper,并调用受保护的2个方法init和setPdfWriter,因此除非您对辅助程序进行子类化并将这些方法公开给您的子类,否则您的子类将无法编译。我这样做了:

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

然后,我称之为:

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

exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
exporter.exportReport();

答案 1 :(得分:1)

在JasperReports 6.x中,您可以通过设置

在报告模板(jrxml)中分别指定偶数页和奇数页的边距
<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"/>

可以从JasperReports示例文件demo/samples/query/reports/QueryReport.jrxml中找到一个示例。我在an issue中找到了这个解决方案。

在Java中将报告导出为pdf时,可以使用JRPdfExporter类设置相同的内容:

JRPdfExporter exporter = new JRPdfExporter();
SimplePdfReportConfiguration configuration = new SimplePdfReportConfiguration();
configuration.setOddPageOffsetX(10);
configuration.setEvenPageOffsetX(-10);
exporter.setConfiguration(configuration);

答案 2 :(得分:0)

除了@bigspotteddog的答案之外,为了使用jasper 5.6,我做了:

@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;
}

和:

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

在循环中。