使用c#中的iTextSharp的pdf阿拉伯语 [英] Arabic in pdf using iTextSharp in c#

查看:173
本文介绍了使用c#中的iTextSharp的pdf阿拉伯语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a PDF file with Arabic text content in C#. I'm using iTextSharp to create this. I followed the instruction in http://geekswithblogs.net/JaydPage/archive/2011/11/02/using-itextsharp-to-correctly-display-hebrew--arabic-text-right.aspx. I want to insert the following Arabic sentence in pdf.


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

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

[●]需要用动态英文单词代替。我尝试使用ARIALUNI.TTF实现这一点[本教程链接建议]。代码如下所示。

The [●] need to be replaced by dynamic English words. I tried to implement this by using ARIALUNI.TTF [This tutorial link suggested it]. The code is given below.

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,如下所示。

While calling this function, I got a PDF with some Unicode as given below.


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

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

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

It is not matching with our hard coded Arabic sentence. Is this a issue of font? Please help me or suggest me any other method to implement the same.

推荐答案

@csharpcoder有正确的想法,但他的执行是关闭。他没有将单元格添加到表格中,并且表格不会在文档中结束。

@csharpcoder has the right idea, but his execution is off. He doesn't add the cell to a table, and the table doesn't end up in the document.

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字符的文本。

You will probably want to get rid of the cell borders etc, but that information can be found elsewhere on SO or the iText website. iText should be able to handle text that contains both RTL and LTR characters.

我认为来源问题实际上是在Visual Studio和Firefox(我的浏览器)中如何呈现阿拉伯语文本,或者是如何连接字符串。我对阿拉伯语文本编辑器不是很熟悉,但如果我们这样做,文本似乎正确:

I think the source problem is actually with how the Arabic text is rendered in Visual Studio and in Firefox (my browser), or alternatively with how the Strings are concatenated. I'm not very familiar with Arabic text editors, but the text seems to come out correctly if we do this:

仅供参考我必须截取屏幕截图,因为从VS复制粘贴到浏览器中(反之亦然)会弄乱文本部分的顺序。

FYI I had to take a screenshot, because copy-pasting into the browser from VS (and vice versa) messes up the order of the parts of the text.

这篇关于使用c#中的iTextSharp的pdf阿拉伯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆