10

Hi
I know that many people may have asked this question before. I've read almost all of them`but it couldn't help me solve my problem.
I'm using iText java library to generate a Persian PDF. I'm using the following code:

Document document = new Document(PageSize.A4,50,50,50,50);
FileOutputStream fos = new FileOutputStream("D:\\ITextTest.pdf");
PdfWriter writer = PdfWriter.getInstance(document,fos);
document.open();
BaseFont bf = BaseFont.createFont("C:\\Windows\\Fonts\\XB YagutBd.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font a = new Font(bf,10);
Paragraph p1 = new Paragraph("سلام دوست من");
p1.setFont(a);
document.add(p1);
document.close();

But when I execute the code, nothing has been written to the PDF file and it's blank. Note that "XB YagutBd.ttf" is a Persian Unicode font and "p1" contains some Persian characters.

What should I do? I've gotten stuck in this problem... help me please.

Reza Namvar
  • 129
  • 11
  • Try setting the font at the creation of the paragraph instead of later on: `Paragraph p1 = new Paragraph("سلام دوست من", a);` – Chris Haas Jul 10 '14 at 20:28
  • Dear @ChrisHaas it worked for me! I can see the characters now. But I have another problem now... The characters are shown separated and not "connected" as it should be in Persian orthography :( – Reza Namvar Jul 11 '14 at 10:03

1 Answers1

5

Only some of the iText elements support RTL, shuch as PdfPCell, PdfPTable, ColumnText. Only these elements have RunDirection property which can be set to PdfWriter.RUN_DIRECTION_RTL value. (more info in Persian)

VahidN
  • 18,457
  • 8
  • 73
  • 117
  • dear Vahid, I took a look at your website "dotnettips" and it was awesome! But I'm using java iText but your advices were about C#. But I tried to implement them in java. Can you please send me an email and have a conversation about it? rthenamvar@gmail.com – Reza Namvar Jul 11 '14 at 10:08
  • Java and C# ports of iText/iTextSharp are very similar. In Java version you have `p1.setFont(a)`, in C# version you will have `p1.Font = a` and so on. – VahidN Jul 11 '14 at 10:46
  • Dear Vahid, yes they are similar really! And now it works for me!! Just another question: The text is left-aligned although I did whatever you said in your tutorial at "dotnettips". Don't you know how can I take it to the right side of the page?! – Reza Namvar Jul 11 '14 at 14:42
  • You can wrap it inside the `PdfPTable` with RunDirection = PdfWriter.RUN_DIRECTION_RTL. Or you can try `ColumnText` objects. – VahidN Jul 11 '14 at 15:13