PDF格式的希伯来文

时间:2011-05-25 20:27:38

标签: c# winforms pdf itextsharp

我写了一个PDF文档,我尝试用希伯来语(UTF-8)编写,我不能在Windows窗体中使用C#和Visual Studio 2010使用以下代码。

Document Doc = new Document(PageSize.LETTER);

//Create our file stream
using (FileStream fs = new FileStream("C:\\Users\\moshe\\Desktop\\Test18.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("C:\\Users\\moshe\\Desktop\\proj\\gold\\fop\\gold", "ARIAL.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);

    //Write some text
    Doc.Add(new Phrase("מה קורה", f));

    //Write some more text
    Doc.Add(new Phrase("תודה לכולם", f));

    //Close the PDF
    Doc.Close();

我把字体放在文件夹中。

我需要做什么?

1 个答案:

答案 0 :(得分:11)

使用PdfPTable,然后您可以设置从右向左模式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            Document Doc = new Document(PageSize.LETTER);

            //Create our file stream
            using (FileStream fs = new FileStream(@"C:\Users\moshe\Desktop\Test18.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 Arial file
                string ARIALUNI_TFF = Path.Combine(@"C:\Users\moshe\Desktop\proj\gold\fop\gold", "ARIAL.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);

                //Use a table so that we can set the text direction
                PdfPTable T = new PdfPTable(1);
                //Hide the table border
                T.DefaultCell.BorderWidth = 0;
                //Set RTL mode
                T.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                //Add our text
                T.AddCell(new Phrase("מה קורה", f));

                //Add table to document
                Doc.Add(T);

                //Close the PDF
                Doc.Close();

            }
        }
    }
}
相关问题