iTextSharp显示阿拉伯语

时间:2016-05-16 19:58:17

标签: c# pdf-generation itextsharp arabic-support

我正在使用iTextSharp(5.5.9)创建一个包含阿拉伯语和英语字符的pdf页面,我使用PdfPTable来组织页面中的文本。我能够展示英文字符,但阿拉伯语没有运气。

我使用MVC6 web api将文档加载到浏览器,他是代码:

[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;
    }

但我得到问号

1 个答案:

答案 0 :(得分:2)

您的代码中存在一些错误。而不是细胞 你必须在你编写的代码中放置表

cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;