JasperReports logo version 7.0.0 Home - Samples - Configuration - Functions - FAQ - API (Javadoc)

JasperReports - Paragraph Sample

Shows how to work with paragraphs.

Main Features in This Sample

Paragraphs

Paragraphs

Documented by Sanda Zaharia

Description / Goal
How to format texts using paragraph properties.

Since: 4.0.2

Working with Paragraphs

When we place text content in a report element, we may need to make it look like a distinct paragraph. The information needed in this case can be retrieved either from the element's style (where we have attributes for text alignment), or from the <paragraph/> tag that is available within <style/> and/or <element/>.

Example:

  <element kind="textField" hTextAlign="Right">
    <paragraph lineSpacing="Fixed" lineSpacingSize="20.0">
      <tabStop position="20" alignment="Center"/>
      <tabStop position="40" alignment="Center"/>
    </paragraph>
  <expression>...</expression>
</element>

Paragraph Alignment

A paragraph can be aligned both horizontally and vertically.
For the horizontal alignment we can choose one of the following options:

Possible values for the above settings are stored in HorizontalTextAlignEnum class:

For the vertical alignment, the following options are available:

Possible values for the vertical text alignment are stored in VerticalTextAlignEnum class:

Examples:

<style name="MyParagraphStyle" hTextAlign="Center" vTextAlign="Middle" ... />
...
 <element kind="textField" style="MyParagraphStyle"...>
  <expression>...</expression>
 </element>

or:

 <element kind="textField"  hTextAlign="Center" vTextAlign="Middle"...>
  <expression>...</expression>
 </element>

or:

 <element kind="textField" ...>
  <expression>...</expression>
  <property name="net.sf.jasperreports.style.hTextAlign" value="Center"/>
  <property name="net.sf.jasperreports.style.vTextAlign" value="Middle"/>  
 </element>

Line Spacing

For a given paragraph one can apply various settings for line spacing, as attributes of the <paragraph/> tag associated with the text element:

Example:

  <element kind="textField" style="indentStyle" ...>
    <paragraph lineSpacing="Fixed" lineSpacingSize="20.0" style="indentStyle">
      <tabStop position="33" alignment="Center"/>
    </paragraph>
    <expression>...</expression>
  </element>

Paragraph Indentation

JasperReports also supports paragraph indentation, which can be customized using the following attributes of the <paragraph/> tag:

Example:

  <style name="indentStyle" mode="Transparent" backcolor="#FFFFFF" style="myDefault">
    <paragraph firstLineIndent="20" leftIndent="35" rightIndent="20" style="myDefault"/>
    <box style="myDefault">
      <pen lineWidth="1.0"/>
    </box>
  </style>

Tab Stops

In a given text element we can configure custom tab stops as well, by placing a sequence of <tabStop/> elements inside a <paragraph/> tag. A custom tab stop is completely characterized by its position and alignment:

Another useful setting is the tab stop width (in pixels) of the paragraph, that can be customized using the tabStopWidth attribute of the <paragraph/>.

Examples:

<paragraph lineSpacing="Fixed" lineSpacingSize="20.0" tabStopWidth="20"/>

or

<paragraph lineSpacing="Fixed" lineSpacingSize="20.0">
  <tabStop position="20"/>
  <tabStop position="40" alignment="Center"/>
  <tabStop position="80" alignment="Right"/>
</paragraph>

Default Values for Paragraph Settings

In case we need the same paragraph settings to be applied for all reports that share the same context, we can use the following properties that can be set on the report context:

Following are the default values as stored in the default.jasperreports.properties file:

net.sf.jasperreports.default.line.spacing.size=1
net.sf.jasperreports.default.first.line.indent=0
net.sf.jasperreports.default.left.indent=0
net.sf.jasperreports.default.right.indent=0
net.sf.jasperreports.default.spacing.before=0
net.sf.jasperreports.default.spacing.after=0
net.sf.jasperreports.default.tab.stop.width=40

Running the Sample

Running the sample requires the Apache Maven library. Make sure that maven is already installed on your system (version 3.6 or later). In a command prompt/terminal window set the current folder to demo/samples/paragraphs within the JasperReports source project and run the following command:

> mvn clean compile exec:exec@all

It will generate all supported document types containing the sample report in the demo/samples/paragraphs/target/reports directory.




© 2001- Cloud Software Group, Inc. www.jaspersoft.com