6

I wrote a PDF document, and I try to write in Hebrew (UTF-8), and I can not in Windows Forms using C# and Visual Studio 2010 using the following code.

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();

I put the font in the folder.

What do I need to do?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mnaftal
  • 185
  • 1
  • 2
  • 7

1 Answers1

12

Use a PdfPTable, then you can set the right-to-left mode:

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();

            }
        }
    }
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • @Mark Storer, I tried setting it on my `PdfWriter` object itself but it didn't seem to do anything, at least with a `Phrase` and a `Paragraph`. – Chris Haas May 26 '11 at 12:53
  • Looking more at the docs for iText they say that the PdfWriter.SetRunDirection is just a placeholder method and that's what the source code for iTextSharp says, too. – Chris Haas May 26 '11 at 13:03
  • And PdfWriter.getRunDirection() is never called in the source either. ColumnText.get/setRunDirection is used, but that's an instance variable, and there's no way to set a default. Gee, wouldn't it be swell if ColumnText grabbed its initial value from... oh... say... the PdfWriter? I think I have a change to make. Excuse me. – Mark Storer May 26 '11 at 16:24
  • @Mark Storer, then is a table still the only way to write RTL? – Chris Haas May 26 '11 at 16:30
  • Using a ColumnText directly and setting its runDirection will work, but that requires drawing to a PdfContentByte yourself rather than using the Element-based stuff added to a Document. **Short Answer**: yeah, kinda. – Mark Storer May 26 '11 at 16:40
  • Never did get that change in. We're maven-ising iText and it's got my eclipse install all ass-over-elbows. It's a pretty trivial change, I just want to be able to ACTUALLY BUILD THE THING before I check it in. Grr. – Mark Storer Jun 06 '11 at 17:03
  • @ChrisHaas the code does not work if I use a custom Urdu/Arabic font. It just does not display ANY text. My question is here: http://stackoverflow.com/questions/15609734/itextsharp-does-not-render-custom-urdu-font – Volatil3 Mar 25 '13 at 08:56