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");
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?