Search code examples
jasper-reports

How can I make my lines line up, move less then 1px?


In a subreport I have line and I can't line this up perfectly with surrounding elements, even moving it just 1 px each time. It's seems to always 1/2 a pixel off.

Is it possibile to move it less than 1 px?


Solution

  • You can't

    I also find this annoying when trying to get pixel perfect lines to line up, even if I understand the reason, lineWidth/2 = int (px)). My work around is to not use lines but instead borders on text box.

    Example to show the problem and the solution, 2 connected lines (lineWidth=2px vs text box with borders)

    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="Lines" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3f1fba4c-298d-4b77-bf79-04798f7724f3">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <title>
            <band height="50" splitType="Stretch">
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                <line>
                    <reportElement x="0" y="0" width="50" height="1" uuid="1b773b6e-63d4-4003-b145-edbff21586cf">
                        <property name="com.jaspersoft.studio.unit.x" value="px"/>
                        <property name="com.jaspersoft.studio.unit.y" value="px"/>
                        <property name="com.jaspersoft.studio.unit.width" value="px"/>
                    </reportElement>
                    <graphicElement>
                        <pen lineWidth="2.0" lineColor="#FF0400"/>
                    </graphicElement>
                </line>
                <line>
                    <reportElement x="0" y="0" width="1" height="50" uuid="675afe9c-5fee-4cb8-9747-9ca3524e1a4d">
                        <property name="com.jaspersoft.studio.unit.x" value="px"/>
                        <property name="com.jaspersoft.studio.unit.y" value="px"/>
                        <property name="com.jaspersoft.studio.unit.width" value="px"/>
                        <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    </reportElement>
                    <graphicElement>
                        <pen lineWidth="2.0" lineColor="#FF0400"/>
                    </graphicElement>
                </line>
                <staticText>
                    <reportElement x="100" y="0" width="50" height="50" uuid="7c52df48-a692-47fa-a3bf-fca0130785fa">
                        <property name="com.jaspersoft.studio.unit.x" value="px"/>
                        <property name="com.jaspersoft.studio.unit.y" value="px"/>
                        <property name="com.jaspersoft.studio.unit.width" value="px"/>
                        <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    </reportElement>
                    <box>
                        <topPen lineWidth="2.0" lineColor="#FF0400"/>
                        <leftPen lineWidth="2.0" lineColor="#FF0400"/>
                    </box>
                    <text><![CDATA[]]></text>
                </staticText>
            </band>
        </title>
    </jasperReport>
    

    Output

    output