How to change report page direction to "rtl"?

I am creating a report with JasperReports using the generated iReport jrxml file.

My application is multilingual, (English (LTR) and Persian (RTL)). In the generated tables regarding the direction of the text, I need to change the direction of the entire page. Plus I use the locale .

I googled a lot and finally found an attribute JRXlsAbstractExporter.PROPERTY_SHEET_DIRECTION, "RTL", but setting this attribute in excel forced formats has no effect on my report.

    params.put(JRXlsAbstractExporter.PROPERTY_SHEET_DIRECTION, "RTL");
    JasperPrint jasperPrint = JasperFillManager.fillReport(report,params, 
           dataSource != null ? new JRMapArrayDataSource(dataSource) : new JREmptyDataSource());    

the other thing I tried sets this in the exporter's parameters as follows:

    JRExporter exporter = new JRXlsxExporter();
    exporter.setParameter(JRXlsAbstractExporter.PROPERTY_SHEET_DIRECTION, "RTL");
    exporter.exportReport();

but setting this option is not allowed and I get an error.
If you have any experience with how to change the direction of the report page (or, in other words, reflect the entire report in a specific locale), please help.

+4
source share
1 answer

As far as I searched, there is no property, you can use the util class below:

package foo.bar.utils.export;

import java.util.Iterator;
import java.util.List;

import net.sf.jasperreports.engine.JRPrintElement;
import net.sf.jasperreports.engine.JRPrintFrame;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.JasperPrint;

/**
 * Report utilities
 * Please refer to: http://community.jaspersoft.com/questions/523041/right-left-arabic-reports
 * There is another solution at: http://jaspermirror.sourceforge.net/
 * which is not used here
 * @author AFattahi
 *
 */
public class ReportUtils {

    private ReportUtils(){

    }
    /**
     * mirror each page layout
     * @param print
     */
    public static void mirrorLayout(JasperPrint print) {
        int pageWidth = print.getPageWidth();
        for (Object element : print.getPages()) {
            JRPrintPage page = (JRPrintPage) element;
            mirrorLayout(page.getElements(), pageWidth);
        }
    }

    /**
     * mirror a list of elements
     * @param print
     */
    protected static void mirrorLayout(List<?> elements, int totalWidth) {
        for (Iterator<?> it = elements.iterator(); it.hasNext();) {
            JRPrintElement element = (JRPrintElement) it.next();
            int mirrorX = totalWidth - element.getX() - element.getWidth();
            element.setX(mirrorX);

            if (element instanceof JRPrintFrame) {
                JRPrintFrame frame = (JRPrintFrame) element;
                mirrorLayout(frame.getElements(), frame.getWidth());
            }
        }
    }
}

Please note that it JRXlsxExporterdoes not support RTL (it seems to be a bug in version 6), and you shouldJRXlsExporter

exporter = new JRXlsExporter();
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
SimpleXlsReportConfiguration xlsReportConfig = new SimpleXlsReportConfiguration();
xlsReportConfig.setSheetDirection(RunDirectionEnum.RTL);
exporter.setConfiguration(xlsReportConfig);
+1
source

Source: https://habr.com/ru/post/1525228/


All Articles