如何在Java中为Jasper报表设置页边距?

3

如何在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);

InputStream reportStream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/reports/" + "myReport"+".jasper");JasperReport template = (JasperReport) JRLoader.loadObject(reportStream); JasperPrint print = JasperFillManager.fillReport(template, parametarTest, connection);
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(MediaSizeName.ISO_A4);
- Landre
你应该编辑你的帖子,不要把额外信息放在评论中。我已经给出了一个答案,请注意加载jrxml文件而不是jasper文件。 - Petter Friberg
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();JRPrintServiceExporter exporter = new JRPrintServiceExporter(); PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();exporter.setExporterInput(new SimpleExporterInput(print));SimplePrintServiceExporterConfiguration configuration = new SimplePrintServiceExporterConfiguration(); configuration.setPrintRequestAttributeSet(printRequestAttributeSet); - Landre
configuration.setPrintService(defaultPrinter); configuration.setDisplayPageDialog(false); configuration.setDisplayPrintDialog(false); exporter.setConfiguration(configuration); exporter.exportReport(); @legrandviking - Landre
没关系,不需要所有这些代码... 边距已经按照答案设置了... - Petter Friberg
显示剩余5条评论
1个回答

1
jrxml(注意不是.jasper文件)使用JRXmlLoader加载到JasperDesign中。
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可能在设计之外,band可能不适合页面高度,这需要适当处理。
如果目的是将布局移动以适应预印表格,则这是另一个类似的问题,附有完整代码:如何在不同打印机上移动整个布局以适应预印表格

非常感谢您!@PetterFreiberg - Landre

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接