I have created a report to print details. I need to print all the invoiceIds
in the same document and loop through them, but I cannot find a way to do it. How do I
You could change you report (query and structure), to take the List<Integer>
as parameter and then generate all the invoice in same report, to generate a new page for every invoice you group on idInvoice
and set group to page break, <group name="idInvoice" isStartNewPage="true"></group>
.
However a pure java approach to avoid modifying the report is adding all the pages of multiple single id report to a single JasperPrint
object
Example
//Method to add multiple single id report to single JasperPrint
public JasperPrint getMultipleIdReport(List<Integer> idInvoiceList, JasperReport report) throws JRException {
List<JasperPrint> printList = new ArrayList<JasperPrint>();
for (Integer id : idInvoiceList) {
printList.add(getSingleIdReport(id, report));
}
JasperPrint printFinal = null;
for (JasperPrint jp : printList) {
if (printFinal == null) {
printFinal = jp;
} else {
List<JRPrintPage> pages = jp.getPages();
for (JRPrintPage page : pages) {
printFinal.addPage(page);
}
}
}
return printFinal;
}
//Method to get a single id report, similar to your current code
public JasperPrint getSingleIdReport(int idInvoice, JasperReport report) throws JRException {
//fill the parameter and the return the the JasperPrint for single id
return JasperFillManager.fillReport(report, parameters, connection);
}