4

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

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

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

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 نيبو لبق نم مويلا

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

4

4 回答 4

8

@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(我的浏览器)中呈现阿拉伯文本,或者是如何连接字符串。我对阿拉伯语文本编辑器不是很熟悉,但是如果我们这样做,文本似乎会正确输出:

Visual Studio 中的阿拉伯语文本

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

于 2015-12-30T12:38:37.537 回答
5

仅在 ColumnText 和 PdfPTable 中支持从右到左书写和阿拉伯语连字!

试试下面的代码:

    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.EMBEDDED);
    Font f = new Font(bf, 12);
    //Write some text, the last character is 0x0278 - LATIN SMALL LETTER PHI
    Doc.Add(new Phrase("This is a ميسو ɸ", f));

    //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;
    //Close the PDF
    Doc.Close();
}
于 2015-12-30T11:33:04.643 回答
3

我希望这些笔记可以从其他答案中帮助您:

  1. 使用安全代码来实现您的字体:

    var tahomaFontFile =  Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.Fonts), 
        "Tahoma.ttf");
    
  2. 用途BaseFont.IDENTITY_HBaseFont.EMBEDDED性质。

    var tahomaBaseFont = BaseFont.CreateFont(tahomaFontFile, 
         BaseFont.IDENTITY_H, 
         BaseFont.EMBEDDED);
    var tahomaFont = new Font(tahomaBaseFont, 8, Font.NORMAL);
    
  3. 对您的单元格和表格使用PdfWriter.RUN_DIRECTION_RTL, :

    var table = new PdfPTable(1) 
        { 
            RunDirection = PdfWriter.RUN_DIRECTION_RTL 
        };
    
    var phrase = new Phrase("تم إبرام هذا العقد في هذا اليوم [●] م الموافق [●] من قبل وبين .", 
         tahomaFont);
    var cell = new PdfPCell(phrase)
        {
            RunDirection = PdfWriter.RUN_DIRECTION_RTL,
            Border = 0,
        };
    
于 2017-05-18T08:43:20.907 回答
2

我相信你在字符串结构部分的问题,尝试使用下面的代码它对我很好,祝你好运。` public static void GeneratePDF() {

        //Declare a itextSharp document 
        Document document = new Document(PageSize.A4);
        Random ran = new Random();
        string PDFFileName = string.Format(@"C:\Test{0}.Pdf", ran);
        //Create our file stream and bind the writer to the document and the stream 
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFileName, FileMode.Create));
        //Open the document for writing 
        document.Open();
        //Add a new page 
        document.NewPage();


        var ArialFontFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.ttf");
        //Reference a Unicode font to be sure that the symbols are present. 
        BaseFont bfArialUniCode = BaseFont.CreateFont(ArialFontFile, 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 
        var table = new PdfPTable(1)
        {
            RunDirection = PdfWriter.RUN_DIRECTION_RTL,
        };
        //Ensure that wrapping is on, otherwise Right to Left text will not display 
        table.DefaultCell.NoWrap = false;

        ContentObject CO = new ContentObject();
        CO.Name = "Ahmed Gomaa";
        CO.StartDate = DateTime.Now.AddMonths(-5);
        CO.EndDate = DateTime.Now.AddMonths(43);

        string content = string.Format(" تم إبرام هذا العقد في هذا اليوم من قبل {0} في تاريخ بين {1} و {2}", CO.Name, CO.StartDate, CO.EndDate);
        var phrase = new Phrase(content, font);
        //var phrase = new Phrase("الحمد لله رب العالمين", font);
        //Create a cell and add text to it 
        PdfPCell text = new PdfPCell(phrase)
        {
            RunDirection = PdfWriter.RUN_DIRECTION_RTL,
            Border = 0
        };
        //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 = PDFFileName;
        AcrobatReader.Start();
    }
}

public class ContentObject
{
    public string Name { set; get; }
    public DateTime StartDate { set; get; }
    public DateTime EndDate { set; get; }
}

`

于 2017-11-28T07:25:22.253 回答