【发布时间】:2016-09-12 15:01:49
【问题描述】:
很多生成 PDF 的人都需要绑定它们。一个好的装订要求每个其他页面都支持其左右两侧的备用边距大小。我知道 JasperReports 在其 3.x 系列中不支持这一点。 4.x 系列是否支持此功能?
【问题讨论】:
-
您是指 JasperReports 还是 iReport?
标签: jasper-reports
很多生成 PDF 的人都需要绑定它们。一个好的装订要求每个其他页面都支持其左右两侧的备用边距大小。我知道 JasperReports 在其 3.x 系列中不支持这一点。 4.x 系列是否支持此功能?
【问题讨论】:
标签: jasper-reports
您可以通过继承 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,并调用了两个受保护的方法 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();
【讨论】:
在 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);
【讨论】:
除了回答@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;
在循环中。
【讨论】: