How to add indentation on bullet list?

2019-01-28 05:14发布

问题:

I want to add some indentation to my text field. I've already added a bullet style (adding the <li> tag to the text) but it seems that JasperSoft Studio doesn't automatically indentate the text.

Even if setting the "styled" markup and adding a <ul> tag at the top of the paragraph like this:

"<ul><li> Example text 1... </li><li> Example text 2... </li></ul>"

JasperSoft is still not able to manage the <ul> tag (looking at the documentation, unfortunately, also other HTML tags..) so the result is not what I wanted.

At this point I tried to search on different forums and I found that I could create a manual report style and apply that to my text field (following this guide: Indentation in generated PDF using JasperReports) but niether this worked for me.

I also tried to put a manual configuration, thinking that creating a style could not be comfortable for Jaspersoft, and set to the text field a configuration like this:

So, setting as left padding property the 10 px value, all the text should get a small displacement to the right. Setting the first line indentation property at -10 px the first line of the paragraph should automatically get to the 0 px position, based on the margin of the page. This should be all my indentation, and it seems that JasperSoft is understanding this because of this:

Printing the page the result that I get is completely different.

Does anyone know what I do forget?

回答1:

This would be a quick way to make a bullet list without using attribute tag html, that you have correctly defined as not well supported by jasper-reports.

Use the unicode symbol \u2022 to represent the bullet, with the following example text (\n is line break)

"\u2022 Some text on line 1 that gets longer to wrap\n\u2022 This is text on line 2"

Then you can set leftPadding="10" and firstLineIndent="-10" as described in Indentation in generated PDF using JasperReports

Example jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Indentation" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f0ac92f3-35e5-417e-aecd-5c47be379bf8">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="44" splitType="Stretch">
            <textField isStretchWithOverflow="true">
                <reportElement x="0" y="0" width="110" height="40" uuid="3563a40d-d80e-4e09-9d84-d4f1779c1895"/>
                <box topPadding="0" leftPadding="10" bottomPadding="0" rightPadding="0"/>
                <textElement markup="none">
                    <paragraph lineSpacing="Single" firstLineIndent="-10" leftIndent="0" spacingBefore="0"/>
                </textElement>
                <textFieldExpression><![CDATA["\u2022 Some text on line 1 that gets longer to wrap\n\u2022 This is text on line 2"]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Output

Probably however if it was me I would have converted the text to a JRDataSource and used the jr:list component. For an example on how to create a datasource from a String see this How to convert a , separated String to a table layout? and if you instead are wondering how you can manipulate your text to insert the unicode see How can I replace a character with an unicode image?

Edit adding solution preferred by me which avoids firstLineIndent="-10" hack

The solution is to convert the string into a JRDatasource in this example

 new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Arrays.asList($P{testText}.split("\n")))

and then used the jr:list, with <field name="_THIS" class="java.lang.String"/>

Full jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Indentation" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f0ac92f3-35e5-417e-aecd-5c47be379bf8">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <subDataset name="ListDataset" uuid="a6053d26-ad58-4808-ac51-76e62529d7de">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="_THIS" class="java.lang.String"/>
    </subDataset>
    <parameter name="testText" class="java.lang.String">
        <defaultValueExpression><![CDATA["Some text on line 1 that gets longer to wrap\nThis is text on line 2"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="34" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="170" height="20" uuid="a9d62b3a-cdad-4c44-a3e6-8e7688986380"/>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="ListDataset" uuid="a21cd89b-dd5a-4135-90e0-a84ef83bd9dc">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Arrays.asList($P{testText}.split("\n")))]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="20" width="170">
                        <ellipse>
                            <reportElement x="13" y="4" width="4" height="4" backcolor="#000000" uuid="c2a202b7-ca0f-4ce4-a2d8-35b314f8e1ee">
                                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                                <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                            </reportElement>
                        </ellipse>
                        <textField isStretchWithOverflow="true">
                            <reportElement x="30" y="0" width="80" height="20" uuid="31b05be1-9d89-4928-aec8-095e15e66711"/>
                            <textElement textAlignment="Left"/>
                            <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </title>
</jasperReport>

The output is same as above, but we are not hacking with negative values which could create problems on certain exports.



回答2:

Very simple approach if don't need text reflow or:

  • Set "Markup" to none
  • Use \u2022 to get a bullet.
  • Break the text into lines manually
  • Put \u00a0\u00a0\u00a0 (non-breaking space) in front of each line which doesn't have a bullet. This fakes the indent pretty well.

Drawbacks:

  • You have to break the lines manually
  • Doesn't support spacing between paragraphs.