2011-04-21 40 views
5

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

+0

你的意思JasperReports的iReport的还是? – 2011-04-22 18:47:07

回答

4

您可以完成由Dave提到的marginMirroring,通过继承JRPdfExporter,覆盖方法exportReportToStream。不幸的是,您需要将此方法的源代码复制到您的覆盖中。在覆盖中,您将修改页面循环,就像这样:

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

要与碧玉5.6工作之外的答案,@bigspotteddog我所做的:

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

在循环中。

1

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

<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"/> 

一个例子指定单独偶数和奇数页的报告模板(JRXML)利润率从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);