mtassinari Posted November 4, 2009 Share Posted November 4, 2009 Hi everybody!I've noticed a problem while using JasperReports to make printed reports for our application. The problem is, if you have a styled text field or label, and the text between the style tags contains special characters like for example the character '&' , the style tag is not interpreted correctly but rather printed "as is".Is there a way to solve this problem? 'Cause it's quite an issue for us. Link to comment Share on other sites More sharing options...
svenn Posted November 4, 2009 Share Posted November 4, 2009 You need to convert it so that it is interpreted correctly by the style code.For example an "&" needs to be converted to "&". It will then be displayed correctly. Link to comment Share on other sites More sharing options...
mtassinari Posted November 5, 2009 Author Share Posted November 5, 2009 Yes, I guessed it. But when is the substitution needed to be done? I've tried to do it directly in the report by doing something like $F{param}.replaceAll("&","&") but it is not working and just prints plain "&" Link to comment Share on other sites More sharing options...
svenn Posted November 5, 2009 Share Posted November 5, 2009 I assume for the field that you have checked off "Is styled text". We use the same Java method but we do it before we insert the data into the database. It works for us. Link to comment Share on other sites More sharing options...
mtassinari Posted November 5, 2009 Author Share Posted November 5, 2009 Yes, and I set it to use the "style" tag. Would it make any difference if I set it to use html tags? I cannot modify the database nor the way data is inserted into it. Link to comment Share on other sites More sharing options...
mtassinari Posted November 17, 2009 Author Share Posted November 17, 2009 Bumping the thread to ask again if anyone has a solution not requiring to modify how data is stored on the DB, 'cause that cannot be changed. Link to comment Share on other sites More sharing options...
lucianc Posted December 3, 2009 Share Posted December 3, 2009 mtassinariWrote: $F{param}.replaceAll("&","&") That should work (see the attached test report). Can you post a sample illustrating your problem?Regards,LucianCode:<?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test" whenNoDataType="AllSectionsNoDetail"> <detail> <band height="30"> <textField> <reportElement x="0" y="0" width="100" height="20"/> <textElement isStyledText="true"> </textElement> <textFieldExpression><![CDATA["a&<style isBold="true">b</style>".replaceAll("&","&")]]></textFieldExpression> </textField> </band> </detail></jasperReport> Link to comment Share on other sites More sharing options...
mtassinari Posted December 3, 2009 Author Share Posted December 3, 2009 I think that this works because it is directly in the XML template, while my data with the '&' character is stored in the database and retrieved at "compile time" when the template is filled.in one of my subreports I have a text field defined as (1) and "ragione_sociale_azienda" sometime may contain an '&' character. The problem here is that the character would appear within the style tag, and this completely screws it, so what I obtain is somethink like (2) with the "&" explicitly written and the style not parsed correctly.I hope this helps in showing what my problem is.Code:(1) "<style isBold="true">"+$F{ragione_sociale_azienda}.replaceAll("&","&")+"</style>n<style size="8">"+$F{ragione_sociale_sede}+"</style>"(2) <style isBold="true">Centro Trasformazione Rottami Sud di Maione Rosa & C. s.n.c.</style>Post Edited by mtassinari at 12/03/2009 08:49 Link to comment Share on other sites More sharing options...
lucianc Posted December 10, 2009 Share Posted December 10, 2009 I tried your expression in a report using an map collection data source (see below). It worked fine.So I still don't know what happens in your case. Do you have a self-contained test case to reproduce the problem?Regards,LucianCode:Map data = new HashMap();data.put("ragione_sociale_azienda", "Centro Trasformazione Rottami Sud di Maione Rosa & C. s.n.c.");data.put("ragione_sociale_sede", "sede");List dataList = new ArrayList();dataList.add(data); Link to comment Share on other sites More sharing options...
mtassinari Posted December 10, 2009 Author Share Posted December 10, 2009 I'm sorry, but we have no test case to try to reproduce the issue in a controlled environment, nor I have the time to prepare one.One thing I can say, is that we do not fill that parameter using the hashmap, in fact in the report that field is not a "parameter" (those which you call with $P{param_name}) but a database field (which is accessed with $F{field_name}) and it seems that the moment in which the data is loaded in the report and thereby accessible using $F{field_name} is too late to make the character substitution. That is, it is still performed, but the desired effect is not achieved.What you could do to try and reproduce it as close as our case as possible, is to put the string "Centro Trasformazione Rottami Sud di Maione Rosa & C. s.n.c." into a database table field and prepare a small report to fetch that string while applying the substitution.Post Edited by mtassinari at 12/10/2009 15:44 EDIT: I also tried to anticipate the substitution in the database query itself by using REPLACE(ragione_sociale_azienda,'&','&') but it would still print the style tag as is, without correctly parsing itPost Edited by mtassinari at 12/10/2009 15:55 Link to comment Share on other sites More sharing options...
lucianc Posted December 11, 2009 Share Posted December 11, 2009 I don't understand how fetching the text from a DB would change the way JR works. A Java String is a Java String no matter if it comes from a DB, from a literal in the code, from an XML file or so on. I tried what you suggested anyway and it worked fine.I ran out of clues.Regards,Lucian Link to comment Share on other sites More sharing options...
mtassinari Posted December 14, 2009 Author Share Posted December 14, 2009 I do not understand it too. And this is really frustrating for me... Well, personal feelings aside, I guess we'll just need to tell our customers to avoid the "&" symbol in that field, and this will solve the problem (sort of). Post Edited by mtassinari at 12/14/2009 07:53 Link to comment Share on other sites More sharing options...
carlthewinner Posted December 5, 2013 Share Posted December 5, 2013 I had issues in a text field where I was doing a .replaceAll("&", "&") at the end and it wasn't replacing all the & signs.I ended up needing to put the ..replaceAll("&", "&") on every field that I referenced rather than trying to do it to the entire text block.I realize this post is old, but hopefully this helps someone :). I am on iReport 4.0.1. Link to comment Share on other sites More sharing options...
fahad.anjum002 Posted March 12, 2018 Share Posted March 12, 2018 You can use below function to handle this situation. "<style isBold='true'>"+ net.sf.jasperreports.engine.util.JRStringUtil.xmlEncode("Text Field &") + "</style>"Outout: Text Field &You can read more about JRStringUtil. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now