1

I am using iTextSharp (5.5.9) to create a pdf page that has arabic and english characters, I am using PdfPTable to organize the text in the page. I was able to show the English characters, but no luck with the arabic ones.

I am using MVC6 web api to load the document to the browser, he is the code:

[HttpGet]
    [Route("PrintReport")]
    public FileStreamResult GenerateReport(int id)
    {
 MemoryStream workStream = new MemoryStream();
        Document document = new Document(PageSize.A4, 25, 25, 30, 30);
        PdfWriter.GetInstance(document, workStream).CloseStream = false;

        document.Open();
        document.NewPage();
        string fontLoc = @"c:\windows\fonts\tahoma.ttf"; // make sure to have the correct path to the font file
        BaseFont bf = BaseFont.CreateFont(fontLoc, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font f = new Font(bf, 12);

        PdfPTable table = new PdfPTable(3);
        table.DefaultCell.NoWrap = false;

        table.WidthPercentage = 100;
        table.AddCell(getCell("Testing", PdfPCell.ALIGN_LEFT,f));
        table.AddCell(getCell("Text in the middle", PdfPCell.ALIGN_CENTER,f));
        PdfPCell cell = new PdfPCell(new Phrase("مرحبا", f));
        cell.NoWrap = false;
        cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
        table.AddCell(cell);
        document.Add(table);


        document.Close();

        byte[] byteInfo = workStream.ToArray();
        workStream.Write(byteInfo, 0, byteInfo.Length);
        workStream.Position = 0;

        return new FileStreamResult(workStream, "application/pdf");

      }
private PdfPCell getCell(string text, int alignment,Font f)
    {
        PdfPCell cell = new PdfPCell(new Phrase(text,f));
        cell.Padding = 0;
        cell.HorizontalAlignment = alignment;
        cell.Border = PdfPCell.NO_BORDER;
        return cell;
    }

But I am getting Question marks

Mhand7
  • 527
  • 1
  • 3
  • 21
  • I don't think Tahoma is a full unicode font. Check out this answer, I think it goes into enough detail: http://stackoverflow.com/questions/10329863/display-unicode-characters-in-converting-html-to-pdf – Berin Loritsch May 16 '16 at 20:04
  • I will check it, but I also tried arialuni.ttf but it gave me question marks instead of arabic letters. – Mhand7 May 16 '16 at 21:03
  • I'm not 100% sure on this but I don't believe iText swaps unknown characters with the Unicode replacement character (question mark). I just built a quick sample with a Klingon character (`\uF8D0`) against a font I know doesn't support it currently (`Arial Unicode MS`) and received a blank PDF, no question marks. This leads me to believe you are doing some string processing elsewhere (or it is being done on your behalf) that's performing this replacement before iText gets involved. – Chris Haas May 16 '16 at 22:23
  • yah, I suspected that, but I am not sure, VS2015 should not do anything, it happened with me with notePad++ but with Visual studio it was working fine when I used the encoding with HTML pages. I will try to use the letter codes and check if it will appear or not. Thanks – Mhand7 May 17 '16 at 07:11
  • it looks like your suspect is correct, I used the UTF-8 for representing two letters in Arabic (PdfPCell cell = new PdfPCell(new Phrase("\u0645\u0647", f));) and it worked fine. any thoughts of how to debug this issue and force the utf-8 encoding – Mhand7 May 17 '16 at 07:30
  • I just make it a point to enforce UTF encoding throughout the chain. .NET strings are UTF-16 by default, but databases, files, and anywhere you read from a stream can be a place where character encoding can get mangled. – Berin Loritsch May 17 '16 at 12:45
  • To the OP, you don't necessarily want to force UTF-8, you want to force Unicode. UTF-8/16/7/etc. specify the literal byte order for Unicode characters but as @BerinLoritsch pointed out, the moment that they hit .Net itself they'll always be turned into UTF-16 strings (under the current implementation). VS2015 is very Unicode-aware so you're probably always safe there. For Notepad++ you might need to explicitly force the encoding the first time that you are using a file. – Chris Haas May 17 '16 at 13:30
  • it is a problem with Visual studio 2015, I tried the same code with the same itextsharp version using visual studio 2013 and it worked fine. I am not sure what the problem could be, if anyone can point me where to ask that would be great. Thanks – Mhand7 May 18 '16 at 07:11

1 Answers1

2

There is some mistake in your code. instead of cell you have to put table in your code where you have written

cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;