Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasper-report : How can I stretch a text-field by using java code [closed]

I am using jasper report as reporting tool in my application.And I want to make a dynamic text field by using java code.Is there any way to achieve this? Can you please give me some suggestions?

like image 930
Girish K Avatar asked Oct 02 '22 00:10

Girish K


1 Answers

You can do one thing to resize dynamic field. open text field expression write as following "Include content"+ ${parameterName}.

Refer this

Dynamic Text Fields

Text fields are elements with specific stretch behavior. When their content does not fit within their boundaries, the engine either truncate them to fit within the initial dimensions, or recalculates their height in order to make room for all the text content. Text fields are the elements which require dynamic text measurement at runtime and then start the element size recalculation for all the elements in the affected section. After text fields are measured and stretched, all other elements in the same section are resized or repositioned, according to their own stretching attributes.
To decide whether a text field content gets truncated at runtime or no, one can use the isStretchWithOverflow attribute in the <textField/> element. If true, then the text field height will be automatically increased until the whole text content can be displayed. The default value is false.

Notes:

    Text fields with delayed evaluation do not stretch to acquire all the expression’s content. This is because the text element height is calculated when the report section is generated, and even if the engine comes back later with the text content of the text field, the element height will not adapt, because this would ruin the already created layout.
    When filling report templates horizontally, dynamic text fields inside the detail section do not stretch to their entire text content, because this might cause misalignment on the horizontal axis of subsequent detail sections. The detail band actually behaves the same as the page and column footers, preserving its declared height when horizontal filling is used.

Text Truncation

In some situations preserving the initial layout takes precedence over any other runtime modification. If the text content gets wider than the available area, then a truncation mechanism is applied in order to keep only the part of text which fits within the original boundaries. If one choose the text truncation as desired behavior for a text element, a series of custom properties can be set to indicate how to perform the truncation:

    net.sf.jasperreports.text.truncate.at.char - Flag that determines whether text elements are to be truncated at the last character that fits. By default, when the entire text of a text element does not fit the element's area, the text is truncated at the last word that fits the area.
    net.sf.jasperreports.text.truncate.suffix - Contains a suffix for the truncated text. The suffix is appended to the text when truncation occurs. If the property is not defined, no suffix will be used when the text is truncated.
    net.sf.jasperreports.print.keep.full.text - Flag to determine whether the fill process must preserve the original text for text elements that are not able to fit their entire contents. When this property is set, the engine saves the original text in the JRPrintText print text object, along with the index at which the text is to be truncated by the print object. The original text can be then entirely exported to layout insensitive formats such as CSV, XML, Excel.



Text Truncation and Element Stretching Examples

This sample provides some examples of element stretching and text truncations. One can see various combinations between different elements stretching attributes (positionType, stretchType, isStretchWithOverflow):

  <elementGroup>
  <line>
    <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="5" y="5" 
      width="1" height="16" isPrintWhenDetailOverflows="true"/>
    <graphicElement/>
  </line>
  <textField isStretchWithOverflow="true">
    <reportElement x="10" y="5" width="100" height="16" isRemoveLineWhenBlank="true"/>
    <textElement textAlignment="Justified">
      <font size="12"/>
    </textElement>
    <textFieldExpression class="java.lang.String">
      This is a FIRST long chunk of text that will cause the text field to stretch 
      outside its defined height and force other elements to move downwards.
    </textFieldExpression>
  </textField>
  <line>
    <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="135" y="5" 
      width="1" height="16" isPrintWhenDetailOverflows="true"/>
    <graphicElement/>
  </line>
  </elementGroup>

or text truncation properties:

  <staticText>
    <reportElement x="145" y="205" width="130" height="100">
      <property name="net.sf.jasperreports.text.truncate.at.char" value="true"/>
    </reportElement>
    <textElement>
      <font size="10"/>
    </textElement>
      <text>Text elements can also be truncated at the last character that fits 
        the element reserved area; the behavior is triggered by setting a property. 
        This sentence might not fit fully in the space reserved for the element.
      </text>
  </staticText>
like image 52
Sabapathy Avatar answered Oct 13 '22 10:10

Sabapathy