3

I have created a Jasper Report to display text retrieved from DB. The text is in the HTML format. I have a requirement to style and align the text when displaying in PDF. This is a sample HTML text from DB

<html>
<p><b>This is Bold text</b></p>
<p>  This is a paragraph whith indent</p>
<p>This is a paragra with no indent</p>
<p><center>This text should be centered</center></p>
<p><font size="4">This text should be of font size 4</font></p>
</html>

The bold and paragraphs tags works fine. But the font doesn't work for size 4. There is a center tag for a line which has to be aligned center. Looking at the Jasper documentation center tag is not listed in supported tags. http://jasperreports.sourceforge.net/sample.reference/styledtext/index.html

I tried center alignment with styled and rtf markup, it didn't work. Is there any other way I can achieve the text alignment as center for some lines in text field?

Also let me know if I am using the font tag in wrong way.

Divya
  • 127
  • 1
  • 2
  • 11

1 Answers1

3

Font size

I can't see nothing wrong with your font size command, make sure that you have markup="html" and that the " is escaped properly you can try be replacing it with '

This textFieldExpression

<textField>
    <reportElement x="132" y="0" width="126" height="30" uuid="0328a547-49c7-402c-bcb0-ae2a8fba4fb3"/>
    <textElement markup="html"/>
    <textFieldExpression><![CDATA["<p><font size=\"1\">Small</font>&nbsp;<font size=\"3\">Big</font></p>"]]></textFieldExpression>
</textField>

will render

small BIG

Text alignment

with different markup (styled,rtf,html) you can only format how the text looks (color,bold,underline ecc).

This feature is for producing styled text.

Quoting Teodord (jasper report staff)

The alignment is not among supported styling because it is not about how text characters look, but about how you lay them out. We cannot change the alignment of text within the same text field element.

What are you options?

  1. Left padding text with space, you could create your own static method that left pad your text with es. &nbsp; depending on the text length.

  2. Try the <hc:html> component, this will render an image of you html. (not text anymore but image...)

  3. Post process the JasperReport or JasperPrint to move the element... (note it needs to be a separate element)

not much.. so why not do without....

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Thankyou. The font issue is fixed. – Divya Dec 10 '15 at 19:12
  • For the alignment, text indent is working with &nbsp. I tried html component in order to display a table and center alignment. This works fine for fixed data but doesn't work for dynamic data. The html shrinks its size if the content increases dynamically. Is there any other way to display a html table? – Divya Dec 10 '15 at 19:26
  • the problem with the html component is that it renders as image... thats why you see it shrinking (you can also clip it,it will not shrink, but I guess this will not help). There is no easy way in jasper report to display a html table directly, the work around could be 1) Parse it in java, and pass it as crosstable, 2) Parse it in java and use dynamic jasper. – Petter Friberg Dec 11 '15 at 08:01
  • I tried with table element and it worked for my requirement. For the indent when I tried the   in sample jrxml file it worked perfectly fine. When I tried to send the same content thru Java code, it is not creating indentation. This is html string which I tried.`code` "

    Text without indentation

    "+"

           Text with indentation

    "
    – Divya Dec 14 '15 at 18:37
  • The issue with indentation was because of custom font file I was using for my application. The font file was not proper. So I downloaded the font file from different location which resolved the issue – Divya Feb 05 '16 at 18:46