c# - 在 c# 中使用 iTextSharp 在 pdf 中使用阿拉伯语

标签 c# pdf itext arabic arabic-support

我想用 C# 创建一个包含阿拉伯语文本内容的 PDF 文件。我正在使用 iTextSharp 创建它。我按照 http://geekswithblogs.net/JaydPage/archive/2011/11/02/using-itextsharp-to-correctly-display-hebrew--arabic-text-right.aspx 中的说明进行操作.我想在 pdf 中插入以下阿拉伯语句子。

تم إبرام هذا العقد في هذا اليوم [●] م الموافق [●] من قبل وبين .

[●]需要用动态英文单词代替。我尝试通过使用 ARIALUNI.TTF [This tutorial link suggested it] 来实现它。代码如下。

public void WriteDocument()
{
    //Declare a itextSharp document 
    Document document = new Document(PageSize.A4);

    //Create our file stream and bind the writer to the document and the stream 
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"D:\Test.Pdf", FileMode.Create));

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

    //Add a new page 
    document.NewPage();

    //Reference a Unicode font to be sure that the symbols are present. 
    BaseFont bfArialUniCode = BaseFont.CreateFont(@"D:\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    //Create a font from the base font 
    Font font = new Font(bfArialUniCode, 12);

    //Use a table so that we can set the text direction 
    PdfPTable table = new PdfPTable(1);
    //Ensure that wrapping is on, otherwise Right to Left text will not display 
    table.DefaultCell.NoWrap = false;

    //Create a regex expression to detect hebrew or arabic code points 
    const string regex_match_arabic_hebrew = @"[\u0600-\u06FF,\u0590-\u05FF]+";
    if (Regex.IsMatch("م الموافق", regex_match_arabic_hebrew, RegexOptions.IgnoreCase))
    {
        table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
    }

    //Create a cell and add text to it 
    PdfPCell text = new PdfPCell(new Phrase(" : "+"من قبل وبين" + " 2007 " + "م الموافق" + " dsdsdsdsds " + "تم إبرام هذا العقد في هذا اليوم ", font));
    //Ensure that wrapping is on, otherwise Right to Left text will not display 
    text.NoWrap = false;

    //Add the cell to the table 
    table.AddCell(text);

    //Add the table to the document 
    document.Add(table);

    //Close the document 
    document.Close();

    //Launch the document if you have a file association set for PDF's 
    Process AcrobatReader = new Process();
    AcrobatReader.StartInfo.FileName = @"D:\Test.Pdf";
    AcrobatReader.Start();
}

在调用此函数时,我得到了一个带有一些 Unicode 的 PDF,如下所示。

اذه يف دقعلا اذه ماربإ مت dsdsdsdsds قفاوملا م 2007 نيبو لبق نم مويلا

它与我们硬编码的阿拉伯语句子不匹配。这是字体的问题吗?请帮助我或建议我使用任何其他方法来实现相同的方法。

最佳答案

@csharpcoder 的想法是正确的,但他的执行失败了。他没有将单元格添加到表格中,表格也没有最终出现在文档中。

void Go()
{
    Document doc = new Document(PageSize.LETTER);
    string yourPath = "foo/bar/baz.pdf";
    using (FileStream os = new FileStream(yourPath, FileMode.Create))
    {
        PdfWriter.GetInstance(doc, os); // you don't need the return value

        doc.Open();

        string fontLoc = @"c:\windows\fonts\arialuni.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(1); // a table with 1 cell
        Phrase text = new Phrase("العقد", f);
        PdfPCell cell = new PdfPCell(text);
        table.RunDirection = PdfWriter.RUN_DIRECTION_RTL; // can also be set on the cell
        table.AddCell(cell);
        doc.Add(table);
        doc.Close();
    }
}

您可能想要去掉单元格边框等,但可以在 SO 或 iText 网站的其他地方找到这些信息。 iText 应该能够处理同时包含 RTL 和 LTR 字符的文本。

编辑

我认为源问题实际上与阿拉伯文本在 Visual Studio 和 Firefox(我的浏览器)中的呈现方式有关,或者与字符串的连接方式有关。我不太熟悉阿拉伯语文本编辑器,但如果我们这样做,文本似乎会正确显示:

Arabic text in Visual Studio

仅供引用,我必须截图,因为从 VS 复制粘贴到浏览器(反之亦然)会打乱文本各部分的顺序。

关于c# - 在 c# 中使用 iTextSharp 在 pdf 中使用阿拉伯语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528259/

相关文章:

c# - iTextSharp - 段落行高

c# - c#中的字符串操作

pdf - latex 制品类的纸张尺寸问题

php - 在solr结果中隐藏路径(id)

java - 使用 PDFBox 从 PDF 文档中读取特定页面

asp.net - 奇数单元格未添加到 PDF

pdf - 如何为时间戳签名启用 LTV?

c# - File.Move 前面有 File.Delete 时失败

c# - MySql.数据.MySqlClient.MySqlException : 'Unknown column ' Cantidad' in 'field list' '

c# - 搜索和替换 C# 表达式