java 如何在运行时更改页面格式?(jasperreport)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10772240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to Change Page Format in Runtime?(jasperreport)
提问by islamic_programmer
I created a report page with A4 format in iReport4.5 and use in java application.
我在 iReport4.5 中创建了一个 A4 格式的报告页面,并在 java 应用程序中使用。
how to change A4 to A5 format on runtime in java application?
如何在java应用程序的运行时将A4格式更改为A5格式?
采纳答案by Jacob Schoen
Before I show you how to do this, please note that just changing the Page Size is probably not going to give you what your want. It will make the page larger or smaller depending on what you want, put the positioning of elements will not change. IN your case the report may not even compile depending on where you have items laid out.
在我向您展示如何执行此操作之前,请注意仅更改页面大小可能无法满足您的需求。它会根据您的需要使页面变大或变小,放置元素的位置不会改变。在您的情况下,报告甚至可能无法编译,具体取决于您布置项目的位置。
You do have a couple options though:
不过,您确实有几个选择:
- First you could create a second report for the A5 format, and then at run time grab the appropriate report depending on what you want. This is probably the easiest solution, but it does mean you end up with almost 2 identical reports. Meaning any changes in the future you would have to do in two places.
- Second if it is a fairly straight forward report with a typical layout you could use something like Dynamic Jasperto generate your report in java code.
- Lastly you could work directly against the Jasper Report's API to generate your report at run time.
- 首先,您可以为 A5 格式创建第二个报告,然后在运行时根据您的需要获取适当的报告。这可能是最简单的解决方案,但这确实意味着您最终会得到几乎 2 个相同的报告。这意味着将来您必须在两个地方进行任何更改。
- 其次,如果它是一个具有典型布局的相当简单的报告,您可以使用类似Dynamic Jasper 的东西在 Java 代码中生成您的报告。
- 最后,您可以直接使用 Jasper Report 的 API 在运行时生成您的报告。
现在回答你的问题。首先将 JRXml 文件加载到一个
JasperDesign
JasperDesign
对象中://Note JRXMLLoader could also take a File object or
//InputStream instead of a String as the parameter.
JasperDesign design = JRXmlLoader.load("report.jrxml");
Once you have the JasperDesign
you can set the page size to what ever you want. A5 paper from what I can tell is 5.83" × 8.27". To convert this to a size that JasperReports understands multiply each by 72, getting you 420 x 596 (I rounded as we have to set Integers).
拥有后,JasperDesign
您可以将页面大小设置为您想要的大小。据我所知,A5 纸是 5.83" × 8.27"。将其转换为 JasperReports 理解的大小乘以 72,得到 420 x 596(我四舍五入,因为我们必须设置整数)。
design.setPageHeight(596);
design.setPageWidth(420);
From there you cointinue on your exporting adventure as you normally would.
从那里您可以像往常一样继续您的出口冒险。