Search code examples
javajasper-reportsplaintext

How to draw lines between records for plain text output format (.txt)?


My problem is that when i try to export the jasperprint to text, the lines/rectangles doesn't appear in the .txt files though it works with pdf files.

I tried to modify the pen width but nothing appear .

My code is:

JRDesignLine ltest = new JRDesignLine();
ltest.setBackcolor(Color.black);
ltest.setForecolor(Color.black);
ltest.setX(10);
ltest.setY(200);
ltest.setMode(JRDesignStaticText.MODE_OPAQUE);
ltest.setWidth(500);
ltest.setHeight(10);
ltest.setPen(JRDesignLine.FILL_SOLID);
bandHeader.addElement(ltest);

Any suggestions please.


Solution

  • You are asking about the plain text format and it is very simple.

    The problem

    The JRTextExporter is skipping JRLine elements and does not draw the borders (JRLineBox).

    The snippet of JRTextExporter:

    protected void exportElements(List<JRPrintElement> elements) {
        for (int i = 0; i < elements.size(); i++) {
            Object element = elements.get(i);
            if (element instanceof JRPrintText) {
                exportText((JRPrintText) element);
            } else if (element instanceof JRPrintFrame) {
                JRPrintFrame frame = (JRPrintFrame) element;
                setFrameElementsOffset(frame, false);
                try {
                    exportElements(frame.getElements());
                } finally {
                    restoreElementOffsets();
                }
            }
        }
    

    As you can see only the JRPrintText will be prinited. And the protected void exportText(JRPrintText) method does not know anything about Boxes. You can compare it whith the source of JRRtfExporter.exportText(JRPrintText) method.

    Solutions

    • You can try to use rtf output format (JRRtfExporter)
    • Another idea (not elegant and a little bit ugly) is to use staticText or textField (with or without condition) to simulate borders.
    • You can try to set net.sf.jasperreports.export.text.line.separator property. For example you can set value: &#xD;&#xA;_________________________________________________________&#xD;&#xA;. It is \r\n at the start, the some numbers of underscore and \r\n at the end of line. The reports properties can be something like this:
    <property name="net.sf.jasperreports.export.text.page.height" value="66"/>
    <property name="net.sf.jasperreports.export.text.page.width" value="94"/>
    <property name="net.sf.jasperreports.export.text.line.separator" value="&#xD;&#xA;_________________________________________________________&#xD;&#xA;"/>