Implementing Arabic Text Support in PDFs: RTL and Bi-Directional Font Handling

Good day,
I am trying to implement support for Arabic text in a PDF document (RTL direction, bi-directional fonts, etc.). For Word documents, there is an option to set the paragraph direction and specify bi-directional parameters like this:

for (com.aspose.words.Paragraph paragraph : paragraphs) {
    paragraph.getParagraphFormat().setBidi(true);
    for (Run run : paragraph.getRuns()) {
        run.getFont().setName(ARABIC_FONT_NAME);
        run.getFont().setLocaleIdBi(ARABIC_LOCALE_ID);
    }
}

How can I achieve similar functionality in PDFs?

@MindControl

You can add Arabic Text using the code below inside a PDF:

Document pdfDocument = new Document();
Page page =pdfDocument.getPages().add();
TextFragment t1 = new TextFragment("Some Arabic Text");
t1.getTextState().setFont(FontRepository.findFont("Font that Supports Arabic Characters"));
t1.getTextState().setFontSize(12);
page.getParagraphs().add(t1);
1 Like

It doesn’t apply right to left direction for arabic text when I do textFragment.setText().

for (TextFragment textFragment : textFragmentCollection) {
    if (textFragment.getText() != null && !textFragment.getText().trim().isEmpty()) {
        String srcText = textFragment.getText().trim();
        String destText = dictionary.get(srcText); // Ensure the dictionary has Arabic translations
        try {
            if (destText != null) {
                if (targetLocaleId != null) {
                    textFragment.getTextState().setFont(arabicFont);
                }
                textFragment.setText(destText);
            }
        } catch (Exception e) {
            log.error("Replace text failed for srcText={}, destText={}", srcText, destText, e);
        }
    }
}

Also, for some texts it throws an error:

com.aspose.pdf.internal.ms.System.l7k: Format of font "Symbol" is not supported for new composite fonts
	at com.aspose.pdf.internal.l7t.lj.lf(Unknown Source)
	at com.aspose.pdf.internal.l7t.lj.<init>(Unknown Source)
	at com.aspose.pdf.internal.l8j.lf.lI(Unknown Source)
	at com.aspose.pdf.internal.l7t.l1p.lI(Unknown Source)
	at com.aspose.pdf.internal.l7v.lu.lI(Unknown Source)
	at com.aspose.pdf.internal.l7p.lI.lI(Unknown Source)
	at com.aspose.pdf.internal.l7j.lI.lI(Unknown Source)
	at com.aspose.pdf.TextSegment.setText(Unknown Source)
	at com.aspose.pdf.TextFragment.setText(Unknown Source)

This error happens on ubuntu only. On MacOS it’s ok

@MindControl

The issue can be related to the fonts as some fonts are differently distributed for Linux like operating systems. Nevertheless, can you please share the Arabic Text that you are adding and the Font name which you are using? We will log an investigation ticket and share the ID with you.

1 Like

@asad.ali the text I add varies. Sometimes it’s Arabic, and sometimes it’s regular text like ‘Thank you.’
The font used is Noto Naskh Arabic (installed on the Ubuntu system).

@MindControl

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-44368

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

Thank you.

What about RTL for arabic text? Is it supported now?

@MindControl

This also need to be investigated under the same ticket. As soon as we have some results, we will inform you.