2005 IR Help Posted August 22, 2006 Share Posted August 22, 2006 By: Vital - vitalsim Change font size of fields at run-time? 2003-01-07 03:52 I have a report design specified in a XML file. I would like to change the font size of all the fields on my report design at run-time. User would choose the font size that is to be set for the fields. I am using JRXmlLoader class to get the JasperDesign object, but am not sure of how to change the field properties ... Can anybody help me in this regard? Thanks in advance. Vital By: Teodor Danciu - teodord RE: Change font size of fields at run-time? 2003-01-07 04:50 Hi, The best approach would be to not specify the font size for the text elements that required dynamic font size. Instead, you point these text elements to use a report level font definition using the "reportFont" attribute. This will allow you to change the font size by retrieveing the report font definition from the JasperDesign object by calling the getFontsMap() method and using the report font name as key. Then, if you affect the font size in the report font object, all the text elements that make reference to this report font will use the new font size value, but only if they do not override the size as mentioned above. Modifying the font size in the JasperDesign object does not require you to recompile the report design. I hope this helps. Teodor By: Vital - vitalsim RE: Change font size of fields at run-time? 2003-01-07 05:03 Sounds a better approach ... Shall try and let you know the result. Thanks Teodor By: Vital - vitalsim RE: Change font size of fields at run-time? 2003-01-07 06:23 Ok Teodor. Even i shall try for other alternatives and update you if i find one. Thanks, Vital By: Teodor Danciu - teodord RE: Change font size of fields at run-time? 2003-01-07 06:32 Hi, I think in the future we shall introduce the possibility to name the report elements so that you can easily access them by their name when wanting to modify their settings at runtime. Thank you, Teodor By: Vital - vitalsim RE: Change font size of fields at run-time? 2003-01-07 07:16 That should solve most of the issues...i had this question at the back of mind from a long time. Anyhow, thanks for your constant support. Vital By: Vital - vitalsim RE: Change font size of fields at run-time? 2003-01-07 05:46 Teodor, I tried the option suggested by you.. Defined a new report font and applied it to the desired textfields on the report. Though i'm successfull in changing the font size by getting the JRDesignReportFont from the fonts Map, the new font size is not applied to the textfields having this report font. Do I need to set any additional attributes either to the defined report font or the text fields using this font... Following is the reportFont definition: <reportFont name="runtimeFont" isDefault="false" fontName="sansserif" size="5" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="CP1252" isPdfEmbedded="false"/> Static Text using the above report font: <staticText> <reportElement mode="Transparent" x="184" y="75" width="159" height="31" forecolor="#000000" backcolor="#FFFFFF" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <textElement textAlignment="Center" lineSpacing="Single"> <font fontName="sansserif" reportFont="runtimeFont" pdfFontName="Helvetica" size="5" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" /> </textElement> <text><![CDATA[Runtime Font]]></text> </staticText> Java code to change the font size dynamically, JasperDesign jasperDesign = JRXmlLoader.load(fileName); Map fontMap = jasperDesign.getFontsMap(); JRDesignReportFont reportFont = (JRDesignReportFont) fontMap.get("runtimeFont"); reportFont.setSize(25); JasperCompileManager.compileReportToFile(jasperDesign,"RuntimeReport.jasper"); Thanks Vital By: Teodor Danciu - teodord RE: Change font size of fields at run-time? 2003-01-07 06:04 Hi, On a second thought, you might be right. Apparently I have misled you. Sorry ... In fact, the <reportFont> tag acts like an XML simplifier than a real font subclassing mechanism. I forgot about that... I'll study this in detail before jumping into conclusions. Thank you, Teodor By: protifar ipo - protifar RE: Change font size of fields at run-time? 2004-02-11 23:43 hi, how should I do if I want to set the font position dynamically(actually in the run time)? By: protifar ipo - protifar RE: Change font size of fields at run-time? 2004-02-12 00:22 I'm sorry... I mean "text" position rather than "font" position By: Teodor Danciu - teodord RE: Change font size of fields at run-time? 2004-02-14 03:15 Hi, You can change some of the element settings at runtime. Check the "alterdesign" sample and the API documentation to see what settings can be altered without need for report design recompilation. But the modifications you make for an element can be made only prior to launching the report filling process and will affect all the document. In other words, you cannot change a setting with each row in the data source. If you want this, you can only achieve it with this trick here: http://jasperreports.sourceforge.net/tips.tricks.html#formatting I hope this helps. Teodor 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