I am bringing this question from this post. I was not sure if I should create a new question since it seems I was already creating a new topic? Apologies upfront for that.
So here is my problem:
I am trying to dynamically change the format of my report (A book report, with several pages or parts or subreports). What I am doing is loading it first, from an existing template into a JasperDesign Object holder. Then, modifying the size an other parameters of the page itself. Later, I will compile and try to print a PDF format report with the compiled file. Ultimately, I want to print a hole book report with different pages and different templates for each individual pages with a format that I can specify.
Here is what I have done. I was able to point the main report to the other individual pages formats. So, when I export the report to PDF everything looks good(Like you probably imagine it was not that simple to get there. but now is working). So now I want to parametrize the format (A4, LETTER, etc.) on runtime for every single page of the Book Report.
Is this posible? I mean to do this dynamically instead of creating a new static template for each page and format and then creating a mantainence issue? If so which is the way of doing this?
Here is my code for retriving the JasperDesing Object template:
private static JasperDesign getTemplateDesign(String templatePath) throws JRException{
JasperDesign design = JRXmlLoader.load(templatePath);
logger.info("Setting Page Dimessions Format to: " + pageFormat);
if(JRDriver.pageFormat.equals("A3")){
JRDriver.pageWidth=842;
JRDriver.pageHeight=1190;
JRDriver.columnCount=1;
JRDriver.columnWidth=802;
JRDriver.columnSpacing=0;
JRDriver.leftMargin=20;
JRDriver.rightMargin=20;
JRDriver.topMargin=30;
JRDriver.bottomMargin=30;
}
else if (JRDriver.pageFormat.equals("A4")) {
JRDriver.pageWidth=842;
JRDriver.pageHeight=1190;
JRDriver.columnCount=1;
JRDriver.columnWidth=555;
JRDriver.columnSpacing=0;
JRDriver.leftMargin=20;
JRDriver.rightMargin=20;
JRDriver.topMargin=30;
JRDriver.bottomMargin=30;
}
else if (JRDriver.pageFormat.equals("LETTER")) {
JRDriver.pageWidth=612;
JRDriver.pageHeight=792;
JRDriver.columnCount=1;
JRDriver.columnWidth=517;
JRDriver.columnSpacing=0;
JRDriver.leftMargin=20;
JRDriver.rightMargin=20;
JRDriver.topMargin=30;
JRDriver.bottomMargin=30;
}
//Setting twmplate dimesions
design.setPageWidth(JRDriver.pageWidth);
design.setPageHeight(JRDriver.pageHeight);
design.setColumnCount(JRDriver.columnCount);
design.setColumnWidth(JRDriver.columnWidth);
design.setColumnSpacing(JRDriver.columnSpacing);
design.setLeftMargin(JRDriver.leftMargin);
design.setRightMargin(JRDriver.rightMargin);
design.setTopMargin(JRDriver.topMargin);
design.setBottomMargin(JRDriver.bottomMargin);
return design;
}
Here is my code fragment for generating the PDF report:
...
logger.info("Retrieving Design Template..." + reportTemplatePath);
JasperDesign design= getTemplateDesign(reportTemplatePath);
//jasperReport = JasperCompileManager.compileReport(reportTemplatePath);
jasperReport = JasperCompileManager.compileReport(design);
logger.info("Filling Report...");
jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter,new JREmptyDataSource() );//new JREmptyDataSource()
jasperPrint.setPageWidth(JRDriver.pageWidth);
jasperPrint.setPageHeight(JRDriver.pageHeight);
//Create the file dir
File file = new File(reportOuputPath);
file.getParentFile().mkdirs();
//file.delete();
logger.info("Writing PDF..." + reportOuputPath);
JasperExportManager.exportReportToPdfFile(jasperPrint, reportOuputPath);
...
I could include the information for my JRXML static template files. But I don't think that is necessary for now. It can be done if you think otherwise
Please help me and thanks in advance
Hello and thanks for the quick answer. I couldn't find how to comment on you answer so I guess I will have to include it in this post.
First, you were right about not guaranteeing the whole page layout resizing, by just changing the de page dimensions. However, the formats that I was changing to, were very similar (A4 and LETTER), so the impact on the components and fields size perse, was not that big.
Moreover, what I have to do, to actually change every single individual's page of the book format, was loading every single individual page first into a JasperDesing Object holder and next save it , with the modified size into a jasper field, that will be used in the fillup process. Instead of just doing that for the Report Book page itself.
Here is the code:
Thanks for the help