1

I'm trying to create a left to right, Hebrew PDF in memory using iTextSharp LGPL / MPL (4.1.6) and return it to the client. I have this problem:

The text is indeed in hebrew, but is not right to left, and the text is reversed.

The code:

public MemoryStream ExportGrowthPlan()
    {
        var doc = new Document(PageSize.A4);
        var ms = new MemoryStream();

        //Bind PDF writer to document and stream
        PdfWriter.GetInstance(doc, ms);

        //Open document for writing
        doc.Open();

        //Add a page
        doc.NewPage();

        //Path to the Arial file
        var ARIALUNI_TFF = @"Export\Fonts\ARIAL.TTF";

        //Create a base font object making sure to specify IDENTITY-H
        var bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        //Create a specific font object
        var f = new Font(bf, 12);

        //Use a table so that we can set the text direction
        var T = new PdfPTable(1);

        //Hide the table border
        T.DefaultCell.BorderWidth = 0;

        //Set RTL mode
        T.RunDirection = PdfWriter.RUN_DIRECTION_RTL;

        //Add our text
        T.AddCell(new Phrase("מה קורה", f));
        //T.AddCell(new Phrase("What's up?", f));

        //Add table to document
        doc.Add(T);

        //Close the PDF
        doc.Close();

        var newStream = base.CloneStream(ms);
        return newStream;
    }


protected MemoryStream CloneStream(MemoryStream memoryStream)
    {
        var ms = new MemoryStream(memoryStream.ToArray());
        ms.Seek(0, SeekOrigin.Begin);

        return ms;
    }

This code is taken from another SO question, here

The controller then calls this line to return an IActionResult with the MemoryStream back to AngularJS:

return File(ms, "application/pdf", "somefile.pdf");

And here is the pdf output: enter image description here

As you can see, the letters are reversed and the direction is ltr, not rtl. I've tried to use FileStream instead of MemoryStream, but the result was the same - no rtl and reveresed text. I've tried this code with a console application and it works just fine. For some reason, with my project (ASP.NET Core, with controllers that only return data, not views) I get this behavior.

Any suggestions?

Community
  • 1
  • 1
ashilon
  • 1,791
  • 3
  • 24
  • 41
  • The version you mention (iTextSharp LGPL / MPL 4.1.6) is no longer supported. Please be aware that this version should no longer be used in products or products that you sell to customers. Please read the FAQ: [Can iText 2.1.7 / iTextSharp 4.1.6 or earlier be used commercially?](http://developers.itextpdf.com/question/versions-older-than-5) – Bruno Lowagie Jan 18 '17 at 14:57
  • Actually, if you want really good typography, you need iText 7 with the pdfCalligraph add-on. From iText 7 on, the pdfCalligraph add-on automatically detects the language based on the Unicode values of the characters, and it adapts the run-direction accordingly. – Bruno Lowagie Jan 18 '17 at 15:05
  • Thank you Bruno. Our product is a web app, and is not sold, or selling anything and is not commercial in anyway. It's a repository of schools and their pupils. I hope that it's ok for this kind applications. – ashilon Jan 18 '17 at 15:11
  • That's not OK. I see that you are working for the government. By using iTextSharp 4.1.2, you are introducing a liability into the code base of your governmet. – Bruno Lowagie Jan 19 '17 at 13:52
  • 1
    _Actually_ "commercial use" is not precise enough. It's "closed source use", which can be either commercial or non-profit. So non-profit closed source users, who don't use iText in a commercial context, still need to buy a license. #nitpicking – Amedee Van Gasse Jan 19 '17 at 14:24

1 Answers1

0

OK, now this is really weird... I've replaced the iTextSharp.dll to version 4.1.2 and it works. I'm not saying that we're gonna keep using this version, but at least it seems that it is probably specifically related to version 4.1.6.

HTH

ashilon
  • 1,791
  • 3
  • 24
  • 41