Rabu, 29 September 2010

JasperReport Export ke Text

Sumber Tulisan : http://www.coderanch.com/t/62982/open-source/Jasper-Report-Text-Exporter

JRTextExporter uses a very smart algorithm to convert reports in graphics to reports in text. It first creates a virtual text grid based on your report's size and the character dimensiona given to the exporter. Then it places each individual item to the text grid based on the item's location in pixels. 

There are three very important parameters effecting how the exporter converts the output 

1- Width and Height of the reports in pixels 
2- Widths and Heights of the individual report elements (labels etc.) 
3- Character Width and Height parameters of the exporter 


To fit the report to the text grid, you need to make a simple calcuation: 

First find the maximum number of characters that can be fit to a row when you would print the report in text (Let's assume that it is 80 characters long and call this number MAX_CHAR_PER_ROW). 
Then find the maximum number of characters that can be fit to a column when you would print the report in text (Let's assume that it is 44 characters height and call this number MAX_CHAR_PER_COL). 


Then Divide your reports dimensions with these numbers to find the character height and width. LEt's assume the dimensions of the report are REPORT_WIDTH and REPORT_HEIGHT and the corresponding values are 524 and 524. Now the characters dimensions are calculated as: 


CHAR_WIDTH = REPORT_WIDTH / MAX_CHAR_PER_ROW 
CHAR_HEIGHT = REPORT_HEIGHT / MAX_CHAR_PER_COL 

CHAR_WIDTH = 524 / 80 = 6.55 
CHAR_HEIGHT = 524 / 44 = 11.9 


Of course, the resulting values will not be integers most of the time. Then if you want a perfect match, CHAR_WIDTH and CHAR_HEIGHT must be floating numbers. But the original exporter only accepts integers parameters. At this point, use my modified exporter here. I already converted these parameters to float. 

Here is how you may send character dimensions to the exporter. 


exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Float(6.55));//6.55 //6 
exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Float(11.9)); //11//10 



Another notice! Make sure that CHAR_HEIGHT is not lower that the actual heights of the report items. Otherwise, the items will not added to the grid since the exporter truncates their heights to 0 because of the arithmetic operation inside the exporter. Another solution would be using rounding instead of truncation by modifying the exporter a little more. Anyway, I did not need that feature since the character height is ok for my case. 


/** 
* Transforms y coordinates from pixel space to character space. 
*/ 
protected int calculateYCoord(int y) 


int result = Math.round((float)pageHeight * y / jasperPrint.getPageHeight()); 

return result; 


/** 
* Transforms x coordinates from pixel space to character space. 
*/ 
protected int calculateXCoord(int x) 


int result = Math.round((float)pageWidth * x / jasperPrint.getPageWidth()); 

return result; 


0 komentar:

Posting Komentar

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More