简体   繁体   中英

Jasper Report : Aligning text as center in text field using HTML markup

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.

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

小大

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM