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 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 whole 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 possible? I mean to do this dynamically instead of creating a new static template for each page and format and then creating a maintenance issue? If so which is the way of doing this?
Here is my code for retrieving the JasperDesing Object template:
Here is my code fragment for generating the PDF report:
...
...
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
2 Answers:
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:
private static JasperDesign getPageTemplateDesign(String templatePath) throws JRException{
logger.info("Setting format page desing for Template: " + templatePath);
JasperDesign design = JRXmlLoader.load(templatePath);
logger.info("Setting Page Dimessions Format to: " + pageFormat);
if (JRDriver.pageFormat.equals("A4")) {
JRDriver.pageWidth=595;
JRDriver.pageHeight=842;
JRDriver.columnCount=1;
JRDriver.columnWidth=555;
JRDriver.columnSpacing=0;
JRDriver.leftMargin=20;
JRDriver.rightMargin=20;
JRDriver.topMargin=20;
JRDriver.bottomMargin=20;
}
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=20;
JRDriver.bottomMargin=20;
}
//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;
}
private static void reformattingPageTemplate(String path) throws JRException{
logger.info("Getting Design Template from: " + path);
JasperDesign design= getPageTemplateDesign(path);
String destFile=path.replaceAll("jrxml", "jasper");
logger.info("Compiling Re-formated Design Template to: " + destFile);
JasperCompileManager.compileReportToFile(design, destFile);
}
private static void formattingALLPage() throws JRException{
reformattingPageTemplate(reportBookPagesDir + "/FrontCover.jrxml");
reformattingPageTemplate(reportBookPagesDir + "/Copyright.jrxml");
reformattingPageTemplate(reportBookPagesDir + "/TableOfContent.jrxml");
reformattingPageTemplate(reportBookPagesDir + "/Summary.jrxml");
reformattingPageTemplate(reportBookPagesDir + "/Content.jrxml");
reformattingPageTemplate(reportBookPagesDir + "/AppendixWarnings.jrxml");
reformattingPageTemplate(reportBookPagesDir + "/Index.jrxml");
reformattingPageTemplate(reportBookPagesDir + "/BackCover.jrxml");
//reformattingPageTemplate(reportTemplatePath);
}
Thanks for all the help! ;)