Arabic text Issue in DOC file

Hi,
we are facing below issue while inserting Arabic text with English in doc.

As we are using below code to insert data in DOC.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Define a set of font settings for left-to-right text.
builder.Font.Name = "Courier New";
builder.Font.Size = 16;
builder.Font.Italic = false;
builder.Font.Bold = false;
builder.Font.LocaleId = new CultureInfo("en-US", false).LCID;

// Define another set of font settings for right-to-left text.
builder.Font.NameBi = "Arial";
builder.Font.SizeBi = 24;
builder.Font.ItalicBi = true;
builder.Font.BoldBi = true;
builder.Font.LocaleIdBi = new CultureInfo("ar-AR", false).LCID;

// We can use the Bidi flag to indicate whether the text we are about to add
// with the document builder is right-to-left. When we add text with this flag set to true,
// it will be formatted using the right-to-left set of font settings.
builder.Font.Bidi = true;
builder.Write("(شركة شبكتي المتطورة لنظم و حماية المعلومات ذ.م.م)");

// Set the flag to false, and then add left-to-right text.
// The document builder will format these using the left-to-right set of font settings.
builder.Font.Bidi = false;
builder.Write("Testing data for checking");

doc.Save("C:\\Temp\\AddRow\\Font.Bidi.Docm", SaveFormat.Docm);

@RiteshAML10

To address the issue of inserting Arabic text alongside English text in a document using Aspose.Words, you need to ensure that you are correctly setting the font properties for both right-to-left (RTL) and left-to-right (LTR) text.

Here’s a refined approach based on your provided code:

  1. Font Settings for LTR and RTL: You should define separate font settings for English (LTR) and Arabic (RTL) text. This includes specifying the font name, size, and locale.

  2. Using the Bidi Flag: The Bidi flag should be set to true when inserting Arabic text to ensure it is formatted correctly. After inserting the Arabic text, set the Bidi flag to false for the English text.

Here’s an example of how to implement this:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Define font settings for left-to-right text (English)
builder.Font.Name = "Courier New";
builder.Font.Size = 16;
builder.Font.Italic = false;
builder.Font.Bold = false;
builder.Font.LocaleId = new CultureInfo("en-US", false).LCID;

// Define font settings for right-to-left text (Arabic)
builder.Font.NameBi = "Arial";
builder.Font.SizeBi = 24;
builder.Font.ItalicBi = true;
builder.Font.BoldBi = true;
builder.Font.LocaleIdBi = new CultureInfo("ar-AR", false).LCID;

// Insert Arabic text
builder.Font.Bidi = true; // Set Bidi flag for Arabic text
builder.Write("شركة شبكتي المتطورة لنظم و حماية المعلومات ذ.م.م");

// Insert English text
builder.Font.Bidi = false; // Reset Bidi flag for English text
builder.Write("Testing data for checking");

doc.Save("C:\\Temp\\AddRow\\Font.Bidi.Docm", SaveFormat.Docm);

Key Points:

  • Ensure that the NameBi property is set to a font that supports Arabic characters.
  • The Bidi property must be toggled appropriately before inserting text to ensure correct alignment and formatting.
  • After inserting the Arabic text, switch back to LTR settings for any subsequent English text.

This approach should help you avoid overlapping issues and ensure that both Arabic and English texts are displayed correctly in your document. If you continue to experience issues, please check the font settings and ensure that the fonts used support the characters you are trying to display.

@RiteshAML10 Could you please provide the expected output document (you can create it in MS Word)? We will check the issue and provide you more information.

Thanks @alexey.noskov for solution.
But issue is that when we insert English text with Arabic text then below issue is coming in report.

Data added as below:

but distorting in report as below

So. please suggest on it.

@RiteshAML10 Thank you for additional information, but screenshots are not very useful for analysis. Could yo please provide the expected output in DOCX format?

Hello @alexey.noskov thanks.

As I am using below code to insert English data with Arabic text in paragraph format.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Define a set of font settings for left-to-right text.
builder.Font.Name = "Courier New";
builder.Font.Size = 16;
builder.Font.Italic = false;
builder.Font.Bold = false;
builder.Font.LocaleId = new CultureInfo("en-US", false).LCID;

// Define another set of font settings for right-to-left text.
builder.Font.NameBi = "Arial";
builder.Font.SizeBi = 24;
builder.Font.ItalicBi = true;
builder.Font.BoldBi = true;
builder.Font.LocaleIdBi = new CultureInfo("ar-AR", false).LCID;

// We can use the Bidi flag to indicate whether the text we are about to add
// with the document builder is right-to-left. When we add text with this flag set to true,
// it will be formatted using the right-to-left set of font settings.
builder.Font.Bidi = true;
builder.Write("Testing Company (الهدف للملكية الفكرية ذ.م.م) - 50,000 shares (25%) (until 17 April 2005)");

doc.Save("C:\\Temp\\AddRow\\Font.Bidi.Docm", SaveFormat.Docm);

But it is distorted in report as shared below screen shot.

So, please suggest how can fix this issue.

@RiteshAML10 Please try using the following code to get the correct result:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Define a set of font settings for left-to-right text.
builder.Font.Name = "Courier New";
builder.Font.Size = 16;
builder.Font.Italic = false;
builder.Font.Bold = false;
builder.Font.LocaleId = new CultureInfo("en-US", false).LCID;

// Define another set of font settings for right-to-left text.
builder.Font.NameBi = "Arial";
builder.Font.SizeBi = 24;
builder.Font.ItalicBi = true;
builder.Font.BoldBi = true;
builder.Font.LocaleIdBi = new CultureInfo("ar-AR", false).LCID;

builder.Font.Bidi = false;
builder.Write("Testing Company (");
builder.Font.Bidi = true;
builder.Write("الهدف للملكية الفكرية ذ.م.م");
builder.Font.Bidi = false;
builder.Write(") - 50,000 shares (25%) (until 17 April 2005)");

doc.Save(@"C:\Temp\out.docx");

out.docx (7.2 KB)

hello @alexey.noskov,
we want insert text as a paragraph. how can differentiate data with English and Arabic language. As in below given line we insert data at a time.

builder.Write(“Testing Company (الهدف للملكية الفكرية ذ.م.م) - 50,000 shares (25%) (until 17 April 2005)”);

So please insist on it.

hello @alexey.noskov can you please suggest on above query

@RiteshAML10 I am afraid I do not see any other way to get the expected output without inserting the text using several runs, as shown in the code example above. You can try using regular expression or other method to split the original string into pieces.