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

Problems with "setfontName" dynamically


libero

Recommended Posts

Hello, I have the following problem:

I want to know when creating a report, whether a text field can display the text or not and if not I want to set a Unicode font.

For this purpose I use the scriptlet to set the appropriate font on the appropriate text box.
It works far too well.

Unfortunately, while filling all the text fields are always used only the first font set.

This means that the method "jrBaseTextElement.setFontName (UNICODE_FONT) " only works once.

 

BR

libero

Code:
protected void checkUnicodeFont(String str, String reportKey){    try    {	JasperReport jasperReport = (JasperReport) getParameterValue("JASPER_REPORT");	JRBand[] bands = jasperReport.getDetailSection().getBands();	for (JRBand jrBand : bands)	{	    JRElement element = jrBand.getElementByKey(reportKey);	    if (element instanceof JRBaseTextElement)	    {		JRBaseTextElement jrBaseTextElement = (JRBaseTextElement) element;		String reportFontName = jrBaseTextElement.getFontName();		Font reportFont = new Font(reportFontName, Font.PLAIN, 10);		if (reportFont.canDisplayUpTo(str) != -1)		{		    jrBaseTextElement.setFontName(UNICODE_FONT);		}	    }	}    }    catch (Exception e)    {	LogWriter.error(this,  "Couldn't set font '" + UNICODE_FONT                             + "' for the report field '" + reportKey + "'", e);    }}[/code]			
Link to comment
Share on other sites

  • 2 weeks later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hi,

 

You are trying to use the API in a way it was not designed to work. You learned that the hard way.

 

Currently, one way to change the font at runtime is by using conditional styles:

Please check the samples:

http://jasperreports.sourceforge.net/sample.reference/jasper/index.html#conditionalstyles

 

Another way to ensure fonts support a certain language is if you use font extensions and depending on the current report locale (REPORT_LOCALE parameter) the appropriate font is picked up from the font extension, provided that several font families have the same name, but support different locales:

http://jasperreports.sourceforge.net/sample.reference/fonts/index.html#fontextensions

 

You could also try styled text, the font being specified as part of the markup itself, dynamically.

http://jasperreports.sourceforge.net/sample.reference/styledtext/index.html#styledtext

 

I hope this helps.
Teodor

 

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...