我必须使用五种不同的sql查询来准备报告。每个查询都会给出一个报告表。
所以我写了5个jrxml文件,每个文件对应一个上面的查询,带有自己的标题,标题设置,页脚,页面编号等。
现在,我能够将上述每个jrxmls编译,打印并导出为5个不同的pdf。
但是,客户希望将所有报告整理成一个单独的pdf。那是在最后的pdf中,前四页将是报告一,接下来的五页报告两页,然后报告三页,依此类推。
1)如何实现这个目标?
2)每个报告的页码为1 / 4,2 / 4,3 / 4等。其中第二部分即完整页码以评估时间作为报告进行评估。因此,当我将单个pdf(如果可能)整理所有报告时,是否也可以将这些页面重新编号为最终pdf?
基于下面的答案,我在我的java类中做了以下工作,它起作用了:
try
{
JasperReport jreport1 = JasperCompileManager.compileReport(input1);
JasperPrint jprint1 = JasperFillManager.fillReport(jreport1, new HashMap(), new JREmptyDataSource());
//JasperExportManager.exportReportToPdfFile(jprint, "/home/ashutosh/Desktop/desktop/nikunj/JasperTestApp/output/mytest.pdf");
JasperReport jreport2 = JasperCompileManager.compileReport(input2);
JasperPrint jprint2 = JasperFillManager.fillReport(jreport2, new HashMap(), new JREmptyDataSource());
JasperReport jreport3 = JasperCompileManager.compileReport(input3);
JasperPrint jprint3 = JasperFillManager.fillReport(jreport3, new HashMap(), new JREmptyDataSource());
List<JasperPrint> jprintlist = new ArrayList<JasperPrint>();
jprintlist.add(jprint1);
jprintlist.add(jprint2);
jprintlist.add(jprint3);
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, jprintlist);
OutputStream output = new FileOutputStream(new File("/home/ashutosh/Desktop/desktop/nikunj/JasperTestApp/output/mytestbatch.pdf"));
exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, output);
exporter.exportReport();
}catch(Exception e)
{
e.printStackTrace();
}
上图:input1,input2,input3是输入jrxmls的字符串路径
我的JRXML文件只打印三条消息:Hello World 1,Hello World 2,Hello World 3.
<?xml version="1.0"?>
<!DOCTYPE jasperReport
PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="Simple_Report">
<detail>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="200" height="20"/>
<text><![CDATA[Hello World One!]]></text>
</staticText>
</band>
</detail>
</jasperReport>
感谢阅读!
答案 0 :(得分:13)
您可以利用导出整个jasperprint列表:
List jpList = new ArrayList();
jpList.add(JRLoader.loadObjectFromFile("build/reports/Report1.jrprint"));
...
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, jpList);
exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, stream);
exporter.exportReport();
答案 1 :(得分:5)
这个答案是为了帮助用户 JASPER REPORT VERSION&gt; 5.6 (最新 版本),因此删除已弃用的代码。
由于jasper-report 5.6 JRPdfExporterParameter.JASPER_PRINT_LIST
为deprecated,因此Wojtek Owczarczyk answer的当前代码为:
List<JasperPrint> jpList = new ArrayList<>();
//add your JasperPrint's from loading jrprint or more
//commonly filling report with JasperFillManager.fillReport
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(jpList)); //Set as export input
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(stream)); //Set output stream
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
//set your configuration
exporter.setConfiguration(configuration);
exporter.exportReport();
答案 2 :(得分:1)
没有itext的页码...
private void drawPageNumbers(List<JasperPrint> listJasperPrint, int totalPages) throws JRException {
int pageCount = 0;
int posY = 0;
int posX = 0;
for (JasperPrint jasperPrint : listJasperPrint) {
if (jasperPrint.getOrientation() == JRReport.ORIENTATION_PORTRAIT) {
posY = 805;
posX = 472;
}
if (jasperPrint.getOrientation() == JRReport.ORIENTATION_LANDSCAPE) {
posY = 558;
posX = 717;
}
for (Object obj : jasperPrint.getPages()) {
pageCount++;
JRPrintPage page = (JRPrintPage) obj;
JRPrintText textTotalPages = new JRTemplatePrintText(new JRTemplateText(
jasperPrint.getOrigins()[0], jasperPrint.getDefaultStyleProvider()));
textTotalPages.setX(posX + 54);
textTotalPages.setY(posY);
textTotalPages.setWidth(40);
textTotalPages.setHeight(16);
textTotalPages.setText(" " + totalPages);
page.addElement(textTotalPages);
JRPrintText textPageNumber = new JRTemplatePrintText(new JRTemplateText(
jasperPrint.getOrigins()[0], jasperPrint.getDefaultStyleProvider()));
textPageNumber.setX(posX);
textPageNumber.setY(posY);
textPageNumber.setWidth(80);
textPageNumber.setHeight(16);
textPageNumber.setText("Página " + pageCount + " de");
page.addElement(textPageNumber);
}
}
return;
}
答案 3 :(得分:0)
(iReport示例)
第一部分:
第2部分)
这个概念对我有用。 根据参数,您可以打开或关闭不同的波段。
答案 4 :(得分:0)
您可以尝试这个
JasperPrint jp1 = JasperFillManager.fillReport(reportFile1,reportParams,Connection);
JasperPrint jp2 = JasperFillManager.fillReport(reportFile2,reportParams,Connection);
for (int j = 0; j < jp1.getPages().size(); j++) {
//Add First report to second report
jp2.addPage((JRPrintPage) jp1.getPages().get(j));
}