How to change page sizing/format when load and existing template into JasperDesign and then changed it?

0

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: 

private static JasperDesign getTemplateDesign(String templatePath) throws JRException{
 
JasperDesign design = JRXmlLoader.load(templatePath);
logger.info("JASPER DESIGN  WIDTH BEFORE: " + design.getPageWidth());
logger.info("JASPER DESIGN  HEIGTH BEFORE: " + design.getPageHeight());
 
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=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);
 
logger.info("JASPER DESIGN WIDTH AFTER: " + design.getPageWidth());
logger.info("JASPER DESIGN  HEIGTH AFTER: " + design.getPageHeight());
 
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("JASPER REPORT WIDTH BEFORE: " + jasperReport.getPageWidth());
logger.info("JASPER REPORT HEIGTH BEFORE: " + jasperReport.getPageHeight());
 
logger.info("Filling Report...");
jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter,new JREmptyDataSource() );
 
logger.info("JASPER PRINT WIDTH BEFORE: " + jasperPrint.getPageWidth());
logger.info("JASPER PRINT HEIGTH BEFORE: " + jasperPrint.getPageHeight());
 
jasperPrint.setPageWidth(JRDriver.pageWidth);
jasperPrint.setPageHeight(JRDriver.pageHeight);
logger.info("JASPER PRINT WIDTH AFTER: " + jasperPrint.getPageWidth());
logger.info("JASPER PRINT  HEIGTH AFTER: " + jasperPrint.getPageHeight());
 
//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

alestar's picture
61
Joined: Jan 25 2016 - 1:19pm
Last seen: 1 year 6 months ago

2 Answers:

0

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! ;)

alestar's picture
61
Joined: Jan 25 2016 - 1:19pm
Last seen: 1 year 6 months ago
1

Try setting the width and height on the JasperDesign object (before compiling it) and not on the JasperPrint object.

But even that would not change the size/width of elements inside reports.

Regards,

Lucian

lucianc's picture
7075
Joined: Jul 17 2006 - 1:10am
Last seen: 1 week 3 days ago
Feedback
randomness