Text alignment issue with report generated as PDF

2019-01-29 02:07发布

问题:

I have the following data to be printed in the PDF,

101 HARRIER WAY<br>OMVILLE<br>BELLSHIRE<br>OM1  1HA<br>

It needs to be displayed in the following way,

101 HARRIER WAY
OMVILLE
BELLSHIRE
OM1  1HA

But is is printing like the following,

UPDATE:

When I use other text in the place of OMVILLE say

101 HARRIER WAY<br>HELLO WORLD BANGALORE<br>BELLSHIRE<br>OM1  1HA<br>

it works well. I don't have any idea why it is not working when I give OMVILLE

Code:

<?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="accountStatement" pageWidth="720" pageHeight="1008" columnWidth="680" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isTitleNewPage="true">
    <parameter name="address" class="java.lang.String"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="914" splitType="Stretch">
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="117" y="141" width="297" height="105" isRemoveLineWhenBlank="true"/>
                <textElement markup="html">
                    <font size="9" isBold="false" pdfFontName="Helvetica"/>
                    <paragraph lineSpacing="Single" tabStopWidth="60"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{address}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

I am running this code using iReport 4.0.2 and my JasperReports version is 4.

For running the above code use all the input parameters as default values and for address parameter give below one as input.

101 HARRIER WAY<br>OMVILLE<br>BELLSHIRE<br>OM1  1HA<br>

P.S: I am generating the jrxml in PDF format.

回答1:

Your sample works fine with this versions of JasperReports API: 4.0.1 and 4.1.1, but I've faced the same problem as you with 4.0.2 version.

What is difference?

We can compare two versions of implementation of JRPdfExporter, for example the 4.1.1 and 4.0.2 versions.

As you can see the source code of PdfTextRenderer is different for this two versions, for example the method draw is changed. This method draw() contains the calling of ColumnText.go() method of iText framework. This method is using for drawing text in pdf document.

Possible solutions:

  1. You can upgrade your vestion of JasperReports library. The last one is 5.2.0 and your sample works with it. Your sample works well even with 4.1.1 as I mentioned before

  2. You can fix the source code of PdfTextRenderer class if you can not upgrade the version of JR library.


About testing your issue

I've wrote a small sample for testing your issue. I've used the Maven project - for simple switching between JR libraries versions.

The source code of Java class for testing issue:

public static void testReport() throws JRException {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("address", "101 HARRIER WAT<br/>OMVILLE<br/>BELLSHIRE<br/>OM1 1HA<br/>");

    JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

    JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
} 

And the "slim" test jrxml file was:

<?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="accountStatement" pageWidth="720" pageHeight="1008" columnWidth="680" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isTitleNewPage="true">
    <parameter name="address" class="java.lang.String"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="914" splitType="Stretch">
            <textField>
                <reportElement x="117" y="141" width="297" height="105"/>
                <textElement markup="html">
                    <font size="9" isBold="false" pdfFontName="Helvetica"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{address}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>