Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating Persian PDF with iText

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.

like image 216
Reza Namvar Avatar asked Jul 10 '14 18:07

Reza Namvar


People also ask

How to generate a PDF document from iText text?

com.itextpdf.text.Document : This is the most important class in iText library and represent PDF document instance. If you need to generate a PDF document from scratch, you will use the Document class.

What is iText and how to use it?

The iText library is powerful and supports the generation of HTML, RTF, and XML documents and generating PDFs. We can choose from various fonts to be used in the document. Also, the structure of iText allows us to generate any of the above-mentioned type of documents with the same code.

Can I use iText (Java-PDF library) on my Desktop?

Typically you won’t use it on your Desktop as you would use Acrobat or any other PDF application. Rather, you’ll build iText into your own applications so that you can automate the PDF creation and manipulation process. iText (Java-PDF Library) can be used to: Serve PDF to a browser Generate dynamic documents from XML files or databases

How to use iText library in Maven?

The iText library contains classes to generate PDF text in various fonts, generate tables in PDF document, add watermarks to pages, and so on. There are many more features available with iText which I will leave on you to explore. To add iText into your application, include following maven repository into your pom.xml file.


1 Answers

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)

like image 128
VahidN Avatar answered Oct 02 '22 11:10

VahidN