Wednesday, January 23, 2013
How to apply dynamic colors to jasper report ?
How to apply dynamic colors to jasper report ?
After some googling, i found the way to apply colors dynamically to jasper report.
My working code is,
We can access all the jasper files styles and everything with the help of JasperDesign class.
Just create some styles like header, body and footer and set colors for those style from java file.
The code "styleList[j].setBackcolor(userBackGRDColor);" - sets the back groud color of a style
@Test
public void dynamicColorPdf() {
try {
System.out.println("pdf report creation started .... ");
String outFileNamePDF = "/Users/user/Tried/POC/imageStreamToPdf/dynamicColorPdf.pdf";
new File(outFileNamePDF).getParentFile().mkdirs();
String containerJrxmlFile = "/Users/user/Tried/POC/imageStreamToPdf/dynamicColorPdf.jrxml";
Map<String, Object> parameters = new HashMap<String,Object>();
InputStream reportStream = new FileInputStream(containerJrxmlFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(reportStream);
JasperDesign jasperDesign = JRXmlLoader.load(bufferedInputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
// applying styles
java.awt.Color userForeColor = new java.awt.Color(255, 69, 0);
java.awt.Color userBackGRDColor = new java.awt.Color(255, 0, 0);
JRStyle[] styleList = jasperPrint.getStyles();
System.out.println("styleList.length => " + styleList.length);
for (int j = 0; j < styleList.length; j++) {
System.out.println("styleList[j].getName() => " + styleList[j].getName());
if (styleList[j].getName().equals("style1")) {
styleList[j].setBackcolor(userBackGRDColor);
styleList[j].setForecolor(userForeColor);
jasperPrint.addStyle(styleList[j], true);
} else if (styleList[j].getName().equals("style2")) {
styleList[j].setBackcolor(userBackGRDColor);
styleList[j].setForecolor(userForeColor);
jasperPrint.addStyle(styleList[j], true);
}
}
JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileNamePDF);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.exportReport();
System.out.println("pdf report created .... ");
} catch (Exception e) {
e.printStackTrace();
}
}
Happy coding :)
--
Thank you.
Regards,
Kaleeswaran.S
After some googling, i found the way to apply colors dynamically to jasper report.
My working code is,
We can access all the jasper files styles and everything with the help of JasperDesign class.
Just create some styles like header, body and footer and set colors for those style from java file.
The code "styleList[j].setBackcolor(userBackGRDColor);" - sets the back groud color of a style
@Test
public void dynamicColorPdf() {
try {
System.out.println("pdf report creation started .... ");
String outFileNamePDF = "/Users/user/Tried/POC/imageStreamToPdf/dynamicColorPdf.pdf";
new File(outFileNamePDF).getParentFile().mkdirs();
String containerJrxmlFile = "/Users/user/Tried/POC/imageStreamToPdf/dynamicColorPdf.jrxml";
Map<String, Object> parameters = new HashMap<String,Object>();
InputStream reportStream = new FileInputStream(containerJrxmlFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(reportStream);
JasperDesign jasperDesign = JRXmlLoader.load(bufferedInputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
// applying styles
java.awt.Color userForeColor = new java.awt.Color(255, 69, 0);
java.awt.Color userBackGRDColor = new java.awt.Color(255, 0, 0);
JRStyle[] styleList = jasperPrint.getStyles();
System.out.println("styleList.length => " + styleList.length);
for (int j = 0; j < styleList.length; j++) {
System.out.println("styleList[j].getName() => " + styleList[j].getName());
if (styleList[j].getName().equals("style1")) {
styleList[j].setBackcolor(userBackGRDColor);
styleList[j].setForecolor(userForeColor);
jasperPrint.addStyle(styleList[j], true);
} else if (styleList[j].getName().equals("style2")) {
styleList[j].setBackcolor(userBackGRDColor);
styleList[j].setForecolor(userForeColor);
jasperPrint.addStyle(styleList[j], true);
}
}
JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileNamePDF);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.exportReport();
System.out.println("pdf report created .... ");
} catch (Exception e) {
e.printStackTrace();
}
}
Happy coding :)
--
Thank you.
Regards,
Kaleeswaran.S
Related Posts
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment