this code reads a PDF template and adds text to it. Arabic text in the first image is copied from the PDF template to the database, while the second is read from already existing data. why this behavior?
database is MySql table charset is umbt8mb4 and the collation is umbt8mb4_09000_ai_ci
PdfReader reader = new PdfReader(PdfUtil.class.getClassLoader().getResourceAsStream(docsDownloadConfig.getStatementTemplatePath()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdfDoc = new PdfDocument(reader, writer);
Document document = new Document(pdfDoc, PageSize.A4);
pdfDoc.setDefaultPageSize(PageSize.A4);
String arString=user.getName;;
document.add(new Paragraph(reorderArabicText(arString)).setFont(getFont()));
document.close();
return baos;
private static String reorderArabicText(String text) {
Pattern arabicPattern = Pattern.compile("[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]+");
if (arabicPattern.matcher(text).find()) {
Bidi bidi = new Bidi(text, Bidi.DIRECTION_RIGHT_TO_LEFT);
bidi.setReorderingMode(Bidi.REORDER_INVERSE_NUMBERS_AS_L);
return bidi.writeReordered(Bidi.DO_MIRRORING);
} else return text;
}
private static PdfFont getFont() throws IOException {
return PdfFontFactory.createFont("images/NotoNaskhArabic-Regular.ttf", PdfEncodings.IDENTITY_H);
}
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>8.0.4</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>75.1</version>
</dependency>