I have to generate a report with dynamic content passed by parameters. The problem is that they ask me to adjust different margins according to whether the page is odd or even. I've seen some workarounds to this, where they use $V {PAGE_NUMBER}% 2 == 1
, but in my case, as the content may vary, I don't think it's possible.
Thanks for the response.
Finally I resolved with Java, I get the pages and set X with a condition if is odd or even.
public byte[] generatePdf(Dto dto) throws IOException, JRException {
InputStream inputJasper = null;
JasperPrint print = JasperFillManager.fillReport(inputJasper, parameters, new JREmptyDataSource());
margenPorPagina(print);
report = JasperUtils.export(print, JasperUtils.ReportFormat.PDF);
}
private void margenPorPagina(JasperPrint print) {
List<JRPrintPage> pages = print.getPages();
IntStream.range(0, pages.size()).forEach(index ->
obtenerElementosPorPagina(pages.get(index), index));
}
private void obtenerElementosPorPagina(JRPrintPage page, int contadorPagina) {
List<JRPrintElement> pageElements = page.getElements();
if (contadorPagina%2==0) {
margenPorElemento(pageElements, Constantes.MARGEN_4CM);
} else {
margenPorElemento(pageElements, Constantes.MARGEN_1_5CM);
}
}
private void margenPorElemento(List<JRPrintElement> pageElements, int margen) {
pageElements.stream().forEach(pageElement -> {
pageElement.setX(margen);
pageElement.setWidth(Constantes.LARGO_CAMPO);
});
}