Jump to content
We've recently updated our Privacy Statement, available here ×

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


alestar
Go to solution Solved by alestar,

Recommended Posts

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

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • Solution

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...