2

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

how to use PdfWriter.RUN_DIRECTION_RTL

code:

String ruta = txtruta.getText();
    String contenido= txtcontenido.getText();
    try {
        FileOutputStream archivo = new FileOutputStream(ruta+".pdf");
        Document doc = new Document(PageSize.A4,50,50,50,50);
          PdfWriter.getInstance(doc, archivo);
          doc.open();
          BaseFont bfComic = BaseFont.createFont("D:\\Font\\B Lotus.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
          Font font = new Font(bfComic, 12,Font.NORMAL);
           doc.add(new Paragraph(contenido,font));
       doc.close();
       JOptionPane.showMessageDialog(null,"ok");

    } catch (Exception e) {
        System.out.println("Eroor"+e);    
    }

Output: Problem

Adrino
  • 17
  • 2

3 Answers3

2

Document.add() doesn't support RTL text. You'll have to use ColumnText.setRunDirection or PdfPTable.setRunDirection.

Paulo Soares
  • 1,896
  • 8
  • 21
  • 19
  • I do not know Use ColumnText.setRunDirection , PdfPTable.setRunDirection plz help me . Please write the code for me Thank you – Adrino Jan 27 '16 at 11:32
0

I haven't worked with Persian language. But, I think your problem will be with the font (B Lotus.ttf) you used. In most of times using a registered Unicode font may solve the problem. Try again using a different font.

Also you can RTL a text phrase using following code.

PdfPCell pdfCell = new PdfPCell(new Phrase(contenido, myUnicodePersianFont));
pdfCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

You will find out a similar question here.

Community
  • 1
  • 1
  • If you found this answer is solving your problem, please be kind enough to up vote the answer or mark as “accepted”. Honestly saying, here I'm looking for some reputation in the website. :-) – Indunil Illangasinghe Jan 29 '16 at 02:11
0

I succeeded

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

      JFileChooser dlg = new JFileChooser();
               int option = dlg.showSaveDialog(this);
               if(option==JFileChooser.APPROVE_OPTION){
                  File f = dlg.getSelectedFile();
                  txtaddress.setText(f.toString());
               }
    
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
    String ruta = txtaddress.getText();
    String con= content.getText();
    try {
          FileOutputStream archivo = new FileOutputStream(ruta+".pdf");
          Document doc = new Document(PageSize.A4,50,50,50,50);
          PdfWriter Writer = PdfWriter.getInstance(doc, archivo);
          doc.open();
          LanguageProcessor al = new ArabicLigaturizer();
          Writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
          BaseFont bfComic = BaseFont.createFont("D:\\Font\\titr.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
          Font font = new Font(bfComic, 12,Font.NORMAL);
          Paragraph p = new Paragraph(al.process(con),font);
          p.setAlignment(Element.ALIGN_RIGHT);
          doc.add(p);
          doc.close();
          JOptionPane.showMessageDialog(null,"Yes");
       
    } catch (Exception e) {
           System.out.println("Eroor"+e);    
    }
     
}     
AliReza Abbasian
  • 51
  • 1
  • 1
  • 7
Adrino
  • 17
  • 2