Right-To-Left (RTL) Support Jasper Reports


This article includes the required steps to fix issues related to Right-to-Left (RTL) direction in generated PDF documents from Jasper Reports. Also, it includes the shaping the numbers into Arabic numbers.

  • Add IBM icu4j dependency to your project (it is required for ArabicShaping util class for shaping numbers into Arabic):
		<!-- https://mvnrepository.com/artifact/com.ibm.icu/icu4j -->
		<dependency>
			<groupId>com.ibm.icu</groupId>
			<artifactId>icu4j</artifactId>
			<version>62.1</version>
		</dependency>
  • Add the following class to your project.
public class ReportsUtil{
	public static void fixArabicAlignmentAndNumbers(JasperPrint print) throws ArabicShapingException {
		List pages = print.getPages();
		ArabicShaping shaping=new ArabicShaping(ArabicShaping.DIGITS_EN2AN);
		for (JRPrintPage p: pages) {
			List elements = p.getElements();
			for (JRPrintElement element: elements) {
				if(element instanceof JRPrintText) {
					JRPrintText text = (JRPrintText) element;
					text.setRunDirection(RunDirectionEnum.RTL);
					text.setText(shaping.shape(text.getFullText()));
				}
			}
		}
	}
}

 

  • Call the method on your jasperPrint instance directly after you call fillReport, like the following:
JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(inStream, paramters, connection);
ReportsUtil.fixArabicAlignmentAndNumbers(jprint);

Note: Be sure to format all date fields with  “dd/MM/yyyy” pattern, otherwise the dates will be flipped.