Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

jasper reports - Indentation in generated PDF using JasperReports

I have a piece of HTML stored in a database as:

<ul>
<li>Pretend you're talking to a busy colleague and  have to sum up your   entire question in one sentence: what details can  you include that will help someone identify and solve your problem?</li>
<li>Spelling, grammar and punctuation are important!  Remember, this is the first part of your question others will see - you  want to make a good impression. If you're not comfortable writing in  English, ask a friend to proof-read it for you. </li>
<li>If you're having trouble summarizing the problem, write the title last - sometimes writing the rest of the question first can make it easier to describe the problem.&nbsp;</li>
</ul>

I am displaying this piece of HTML in a PDF using a JasperReports textField, the above HTML should display like this in the generated PDF.

  • Pretend you're talking to a busy colleague and have to sum up your entire question in one sentence: what details can you include that will help someone identify and solve your problem?
  • Spelling, grammar and punctuation are important! Remember, this is the first part of your question others will see - you want to make a good impression. If you're not comfortable writing in English, ask a friend to proof-read it for you.
  • If you're having trouble summarizing the problem, write the title last - sometimes writing the rest of the question first can make it easier to describe the problem. 

But this HTML is showing as :

enter image description here

The snippet from jrxml file:

<textField isStretchWithOverflow="true" isBlankWhenNull="true">
    <reportElement positionType="Float" x="7" y="47" width="501" height="15" isRemoveLineWhenBlank="true"  forecolor="#283234"/>
    <textElement markup="html">
        <font size="10"/>
    </textElement>
    <textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
</textField>

HTML is feeded in description variable.

Any idea how can I align text?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My solution shows the plain JRXML which is the desired result independent from the tools someone is using, e.g. iReport GUI, dynamic reports or java code designing Jasper reports.

First define a style, which corrects the indentation pulling the first line some pixels to the left and pushes the whole box the same width to the right:

<style name="hanging-indentation-style">
    <box leftPadding="23"/>
    <paragraph firstLineIndent="-23"/>
</style>

Second, this style is applied to the reportElement of the textField:

<textField isStretchWithOverflow="true" isBlankWhenNull="true">
    <reportElement style="hanging-indentation-style" positionType="Float" mode="Transparent" x="0" y="0" width="555" height="20" isRemoveLineWhenBlank="true"/>
    <textElement markup="html"/>
    <textFieldExpression class="java.lang.String"><![CDATA[$F{description}]]></textFieldExpression>
</textField>

Depending on your font size you may vary the style values to fit your needs.

I adapted input from Aligning Bullets in Jasper Reports, where dynamic reports api is used, and Jasper Report HTML bullet hanging indent, where it is shown through the GUI, which was not possible in my case using iReport Designer 4.5.1, because there is no option to apply padding directly to a textField.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...