'How to write Arabic text using MigraDoc?

I am using ASP for this and I had to generate reports in PDF format and send the file back to clients so they can download it.

I made the reports using MigraDoc library and they were great but after I tried it with Arabic text I found the texts were in LTR and the characters were disjointed so I made this code to test things out

    ...............
    MigraDoc.DocumentObjectModel.Document reportDoc = new MigraDoc.DocumentObjectModel.Document();
    reportDoc.Info.Title = "test";
    sec = reportDoc.AddSection();
    string fileName = "test.pdf";
    addformattedText(sec, "العبارة", true);
    PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
    renderer.Document = reportDoc;
    renderer.RenderDocument();
    MemoryStream pdfStream = new MemoryStream();
    renderer.PdfDocument.Save(pdfStream);
    byte[] bytes = pdfStream.ToArray();
    ...............


    private void addformattedText(Section sec,string text, bool shouldBeBold = false)
    {
        var tf = sec.AddTextFrame();
        var p = tf.AddParagraph(text);
        p.Format.Font.Name = "Tahoma";
        if (shouldBeBold) p.Format.Font.Bold = true;
    }

I get the output like this test pdf output

I have tried to encode the text and make it a unicode string using this code

 private string getEscapedString(string text)
 {
     if (true || HasArabicCharacters(text))
     {
         string uString = "";
         byte[] utfBytes = Encoding.Unicode.GetBytes(text);
         foreach (var u in utfBytes)
         {
             if (u != 0)
             {
                 uString += String.Format(@"\u{0:x4}", u);
             }
         }
         return uString;
     }
     else
         return text;
 }

and get the returned string into a paragraph and save the PDF documents with unicode parameter set to true

But it is all the same.

I can not figure out how to get it done.

The reports were done using MigraDoc 1.50.5147 library.



Solution 1:[1]

The problem is Arabic language font have 4 different shap in begging,last,connected and alone, where Pdfsharp and MigraDoc can not recognize which shap to print farther more you need to reverse the character order to solve this you can use AraibcPdfUnicodeGlyphsResharper to help do such work as following:

using PdfSharp.Drawing;
using PdfSharp.Pdf;
using AraibcPdfUnicodeGlyphsResharper;
namespace MigraDocArabic
{
internal class PrintArabicUsingPdfSharp
{
    public PrintArabicUsingPdfSharp(string path)
    {
        PdfDocument document = new PdfDocument();
        document.Info.Title = "Created with PDFsharp";
        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        // Create an empty page
        PdfPage page = document.AddPage();

        // Get an XGraphics object for drawing
        XGraphics gfx = XGraphics.FromPdfPage(page);

        // Create a font
        XFont font = new XFont("Arial", 20, XFontStyle.BoldItalic);
        var xArabicString = "????? ?????  ??????? ??? ????".ArabicWithFontGlyphsToPfd();
        // Draw the text
        gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
        gfx.DrawString(xArabicString, font, XBrushes.Black, new XRect(50, 50, page.Width, page.Height), XStringFormats.Center);

        // Save the document...
        document.Save(path);

    }
}

}

Do not Forget the Extension method By the way this is work with iText7 too see the image for result

Result

Solution 2:[2]

PDFsharp does not support RTL languages yet: http://www.pdfsharp.net/wiki/PDFsharpFAQ.ashx#Does_PDFsharp_support_for_Arabic_Hebrew_CJK_Chinese_Japanese_Korean_6

You can work around this limitation by reversing the string.

PDFsharp does not support font ligatures yet. You are probably able to work around this limitation by replacing letters with the correct glyph (start, middle, end) depending on the position.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ibrahem Habash
Solution 2 I liked the old Stack Overflow