arabic encoding in itextsharp

≯℡__Kan透↙ 提交于 2019-12-02 00:49:46

问题


When I'm trying this code to create a PDF in Arabic using C#, the generated PDF file contains discrete characters. Any help so I can't get continuous characters?

//Create our document object
Document Doc = new Document(PageSize.LETTER);

//Create our file stream
using (FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read))
{
    //Bind PDF writer to document and stream
    PdfWriter writer = PdfWriter.GetInstance(Doc, fs);

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

    //Add a page
    Doc.NewPage();

    //Full path to the Unicode Arial file
    string ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arabtype.TTF");

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

    //Create a specific font object
    iTextSharp.text.Font f = new iTextSharp.text.Font(bf, 12, iTextSharp.text.Font.NORMAL);

    //Write some text, the last character is 0x0278 - LATIN SMALL LETTER PHI
    Doc.Add(new Phrase("This is a ميسو ɸ", f));

    //Write some more text, the last character is 0x0682 - ARABIC LETTER HAH WITH TWO DOTS VERTICAL ABOVE
    Doc.Add(new Phrase("Hello\u0682", f));

    //Close the PDF
    Doc.Close();
}

回答1:


Right-to-left writing and Arabic ligatures are only supported in ColumnText and PdfPTable!

Take a look at the following examples:

  • http://itextpdf.com/examples/iia.php?id=205 with result: say_peace.pdf
  • http://itextpdf.com/examples/iia.php?id=215 with result: peace.pdf

Read the book from which these examples are taken to find out why not all words are rendered correctly in peace.pdf

Search http://tinyurl.com/itextsharpIIA2C11 for the corresponding C# version of these examples.

In any case, you need a font that knows how to display Arabic glyphs:

BaseFont bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);

You can now add Arabic text, for instance in a table:

PdfPCell cell = new PdfPCell();
cell.AddElement(new Phrase("Hello\u0682", f));
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;


来源:https://stackoverflow.com/questions/15197040/arabic-encoding-in-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!