Mixing Right-to-Left (RTL) and Left-to-Right (LTR) Text

Technical support for Spire.PDF

Moderator: iceblue support

Mixing Right-to-Left (RTL) and Left-to-Right (LTR) Text

Postby yyp » Fri Jun 30, 2017 10:49 pm

Here's the scenario:

I have some text that includes both LTR (English) and RTL (Hebrew).
When the text renders to the PDf, the RTL text is reversed.

Here's an example.

Dim text as String = "Happy New Year -- שנה טובה"
page.Canvas.DrawString(text, font, brush, x, y)

The result in the PDF file is
"Happy New Year -- הבוט הנש"

I did see this article, but the issue I have is that I have one string which has both LTR and RTL that needs to be rendered accordingly.
If I use PdfStringFormat.RightToLeft, I'll get "raeY weN yppaH" -- שנה טובה.

I also don't know in advance what the string will be, so a suggestion that I first parse the string and then use two statements is not practical. There might be multiple instances in the text that are interspersed with Hebrew RTL.
yyp
 
Posts: 8
Joined: Thu Jun 15, 2017 10:54 pm

Re: Mixing Right-to-Left (RTL) and Left-to-Right (LTR) Text

Postby Betsy.jiang » Mon Jul 03, 2017 3:38 am

Dear yyp,

Sorry for late reply as weekend.
I have noticed the issue with the code page.Canvas.DrawString(text, tfont, PdfBrushes.Black, 10, 10), and forwarded it to our Dev team. We will let you know when there is any progress.
In addition, I found the result is correct("Happy New Year -- שנה טובה") if set PdfStringFormat using the latest Spire.PDF V3.9.189, please check my testing code.
Code: Select all
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();
            string text = "Happy New Year -- שנה טובה";
            System.Drawing.Font font = new System.Drawing.Font("Arial", 12, FontStyle.Regular);
            PdfTrueTypeFont tfont = new PdfTrueTypeFont(font, true);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
            page.Canvas.DrawString(text, tfont, PdfBrushes.Black, 10, 10, new PdfStringFormat() { RightToLeft = true });
            doc.SaveToFile("10986.pdf");

Which version you were using ? And could you please tell us the whole code, OS information and Region information ?

Sincerely,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1433
Joined: Tue Sep 06, 2016 8:30 am

Re: Mixing Right-to-Left (RTL) and Left-to-Right (LTR) Text

Postby Betsy.jiang » Tue Jul 04, 2017 3:11 am

Dear yyp,

I just got information from our Dev team that you need to set the RightToLeft format if there are RTL and LTR language in one statement.

As I said in last post, I could get correct result("Happy New Year -- שנה טובה") on my side. Yet you got wrong result, did you try the latest version ? And could you please offer us the whole code, OS information and Region information for investigation?

Thanks,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1433
Joined: Tue Sep 06, 2016 8:30 am

Re: Mixing Right-to-Left (RTL) and Left-to-Right (LTR) Text

Postby yyp » Fri Jul 07, 2017 7:15 pm

This does seem to work. I'm not sure why earlier I was having this issue. Thanks.

However, there are still other issues when using RightToLeft.
Namely, it seems that some parts of the string are being corrupted. Specifically, any part of the sting that includes "it" or "tt".

Here's an example:

Code: Select all
Dim text as String = "Thank you for your contribution. Better late than never. Happy New Year -- שנה טובה"
Dim format As New PdfStringFormat
format.RightToLeft = True
Dim textWidget As New PdfTextWidget(text, ttf, brush)
textWidget.StringFormat = format

Dim bounds As New Rectangle(point, size)
textWidget.Draw(page, bounds)


The result is
Thank you for your contribu on. Be er late than never. Happy New Year -- שנה טובה


Notice that "contribution" is "contribu on" and "better" is "be er" (missing the ti and tt).

I tested this strange behavior with different strings. "ti"/"tt" are consistently left out. (there may be other string elements that also have this issue, but I did thoroughly test it. These just came to my attention because of the text I happened to be was using.
yyp
 
Posts: 8
Joined: Thu Jun 15, 2017 10:54 pm

Re: Mixing Right-to-Left (RTL) and Left-to-Right (LTR) Text

Postby Betsy.jiang » Mon Jul 10, 2017 3:10 am

Dear yyp,

Sorry for late reply as weekend.
I have tested the code with the latest version. Unfortunately, I didn't reproduce the issue you mentioned. The result on my side is correct, here is my whole code for checking. And I also attach my result document.
Code: Select all
Dim doc As New PdfDocument()
Dim page As PdfPageBase = doc.Pages.Add()
Dim text As String = "Thank you for your contribution. Better late than never. Happy New Year -- שנה טובה"
Dim format As New PdfStringFormat()
format.RightToLeft = True
Dim font As New System.Drawing.Font("Arial", 12, FontStyle.Regular)
Dim tfont As New PdfTrueTypeFont(font, True)
Dim brush As New PdfSolidBrush(Color.Black)
Dim textWidget As New PdfTextWidget(text, tfont, brush)
textWidget.StringFormat = format
Dim bounds As New Rectangle(0, 10, 300, 200)
textWidget.Draw(page, bounds)
doc.SaveToFile("10986-test.pdf")

Could you please provide us with the entire code you were using ? And also provide the OS and Region/Language information ?

Thanks,
Betsy
E-iceblue support team
You do not have the required permissions to view the files attached to this post.
User avatar
Betsy.jiang
 
Posts: 1433
Joined: Tue Sep 06, 2016 8:30 am


Return to Spire.PDF

Who is online

Users browsing this forum: No registered users and 0 guests