learn java
The moose likes Other Open Source Projects and the fly likes Jasper Report - Text Exporter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Jasper Report - Text Exporter " Watch "Jasper Report - Text Exporter " New topic

Jasper Report - Text Exporter

Yaddif Medina

Joined: Oct 05, 2005
Posts: 23
Hi all,

I am working with JasperReport in my web application (generating pdf files) but now my client wants a Text file output. I have tried with the JRTextExporter but it generates just 12 blank lines in the txt file .. (I have tried with the rtf exporter and it gives me a blank file too)!!!.

I am using the jasperreports-1.2.4.jar

PLEASE HELP ME, because I am getting upset for this.

My code is:
JRTextExporter exporter = new JRTextExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "c:\\borra1.txt");
exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer(200));
exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer(50));

Thanks in advance,
Richard Walton

Joined: Jul 01, 2002
Posts: 10

I ahev worked with Jasper Reports for a long while now and in my opinion its a bit tedious to say the least! :-)
Anyways i have moved over to BIRT ( an eclipse project ) it uses Jasper Reports in the back but they seem to have taken the complexity and tediousness out :-)

Well to your solution, what i did is this:

basically just writing to the servletoutput stream which prompts the user to download a file, it works well, let me know ho wyou get on

<a href="http://www.johnboy.co.za" target="_blank" rel="nofollow">http://www.johnboy.co.za</a><br />Change - When the winds of change blow hard enough, the most trivial things can turn into deadly projectiles.
Yaddif Medina

Joined: Oct 05, 2005
Posts: 23
Hi Richard,

I have tried with that way too and nothing change ... I think that the problem is with the Tomcat, because I have installed the iReport and it can export my reports to TXT .... but the Tomcat just send 12 blank lines.

I have copied the whole "lib" directory from iReport to my Tomcat's project and it did not fix it.

I am a little frustrated for this .
Sami Altundag

Joined: Jun 05, 2009
Posts: 1
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 = 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;


It is sorta covered in the JavaRanch Style Guide.
subject: Jasper Report - Text Exporter