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.

1 Like

Is there any solution ? I have the same issue with Arabic text.

@ardak90

The ticket has recently been logged in our issue tracking system and it will be investigated/resolved on a first come first serve basis. As soon as we make some progress towards its resolution, we will inform you. Please be patient and spare us some time.

We are sorry for the inconvenience.

1 Like

Hi Asad, I hope you’re doing well.
I would like to know about the status of the ticket.
Primarily, I am interested in RTL support (for the Arabic language).
Do you have any updates or an estimated timeline for its completion?

Thanks in advance for the information!

@MindControl

We are afraid that the ticket has not been yet resolved due to other issues in the queue. However, your comments have been recorded under the ticket and we will surely inform you as soon as we make some progress towards ticket resolution. Please spare us some time.

We are sorry for the inconvenience.

@asad.ali Do you have any ideas how can I apply RTL manually using the SDK?

@MindControl

There is a property that can be used during PDF generation as below to set LTR or RTL direction of text reading:

Document.Direction = Direction.R2L;

How can it help transform paragraphs so that they go from right to left?

@MindControl

It is only used to set the reading direction of the document. However, for your specific requirements, investigation ticket has already been logged and we will inform you as soon as we make some progress towards ticket resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.