如何在java中将边距设置为jasper报告?

时间:2016-03-24 12:04:30

标签: java jasper-reports

如何在java中将边距设置为jasper报告!

我有带边距的reports.jasper,但是在打印时我必须更改边距!

JasperPrint.setTopMargins(myMarginsValue)无法正常工作!

我使用JRPrintServiceExporter发送给打印机!

当前代码:

InputStream reportStream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/rep‌​orts/" + "myReport"+".jasper"); 
JasperReport template = (JasperReport) JRLoader.loadObject(reportStream);
template.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);

1 个答案:

答案 0 :(得分:0)

使用JasperDesign

jrxml (请注意不是.jasper)加载到JRXmlLoader
JasperDesign design = JRXmlLoader.load(stream); //Location of jrxml file example FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/rep‌​orts/" + "myReport"+".jrxml");
design.setBottomMargin(bottomMargin); //set the margins
design.setTopMargin(topMargin)
design.setLeftMargin(leftMargin)
design.setRightMargin(rightMargin);
design.setColumnWidth(design.getPageWidth()-leftMargin-rightMargin);//if you change your left and right margin you need to set new correct columnWidth

//compile the report
JasperReport report = JasperCompileManager.compileReport(design); //this is what you called template

然后填写,然后导出,如您所愿。

当然textField可能不在设计范围内,如果你将边距增加到很多,乐队可能不适合页面高度,这需要适当考虑。

如果目的是移动布局以适应预先打印的表单,那么这是另一个与完整代码类似的问题:How can I move the whole layout to adapt to pre-printed form on different printers