1

Hi I was trying to display Arabic text in the pdf using pdfbox, but unfortunately I'm unable to get the proper output which was required. I was getting in reverse instead of reading right to left it is reading left to right. Can anyone help me how to read right to left using pdfbox, as I'm new to pdf box.

  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.List;
  import java.util.Properties;

 import org.apache.commons.lang.StringEscapeUtils;
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.pdmodel.PDPage;
  import org.apache.pdfbox.pdmodel.PDPageContentStream;
  import org.apache.pdfbox.pdmodel.common.PDRectangle;
  import org.apache.pdfbox.pdmodel.font.PDFont;
  import org.apache.pdfbox.pdmodel.font.PDType0Font;

  import com.ibm.icu.text.Transliterator;
  import com.itextpdf.text.Document;
  import com.itextpdf.text.DocumentException;
  import com.itextpdf.text.Font;
  import com.itextpdf.text.Paragraph;
  import com.itextpdf.text.Phrase;
  import com.itextpdf.text.pdf.BaseFont;
  import com.itextpdf.text.pdf.FontSelector;
  import com.itextpdf.text.pdf.PdfWriter;

  public class Main {

  static File arialFile = new File(Main.class.getResource("/KacstOffice.ttf").getFile());

  public static void main(String[] args) throws IOException, DocumentException {
      Properties langProperties = new Properties();
      InputStream langSelection = Main.class.getResourceAsStream("/i18n/messages_ar.properties");
      langProperties.load(langSelection);
      File arialFile = new File(Main.class.getResource("/arial.ttf").getFile());
      String ENGLISH_TO_ARBIC = "Latin-Arabic";
      String englishString = (String) base64Decode();
      String unicodeCodes = StringEscapeUtils.escapeJava(englishString);
      System.out.println("Unicode codes:" + unicodeCodes);
      Transliterator chineseToLatinTrans = Transliterator.getInstance(ENGLISH_TO_ARBIC);
      String result1 = chineseToLatinTrans.transliterate(englishString);
      System.out.println("Chinese to Latin:" + result1);
        PDDocument document = new PDDocument();
        PDPage page1 = new PDPage(PDRectangle.A4);
        document.addPage(page1);
        PDPageContentStream contentStream = new PDPageContentStream(document, page1,true, true);
        PDFont font = PDType0Font.load(document, arialFile);
        contentStream.beginText(); 
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(25, 500);
      contentStream.drawString(langProperties.getProperty("pdf.GeneratedOn"));
        contentStream.endText();
        System.out.println("Content added")
        contentStream.close();
        document.save(new File("my_doc1.pdf"));
        document.close();
  }
    public static  Object base64Decode() throws IOException  {
    Properties langProperties = new Properties();
    InputStream langSelection = Main.class.getResourceAsStream("/i18n/messages_ar.properties");
    langProperties.load(langSelection);
    Object  str ="تاريخ الانتهاء"; 
    return str;
  }
}
  • 1
    PDFBox as of now does not support output of RTL languages and/or complex script. Reason is that - although the order could be changed easily - there would still not be proper support as there replacing/choosing the right glyphs for a character dependent on it's appearance at the start/middle/end of the word is also not supported. – Maruan Sahyoun Jun 07 '17 at 09:54
  • okay lets not worry about the RTL, if am giving some word, it is printing some other word in `pdf`, where it is printing correct in console. Little confused, am I doing any mistake ? – Swetha Arvind Jun 07 '17 at 11:05
  • what are you entering, what is displayed? – Maruan Sahyoun Jun 08 '17 at 13:42
  • 1
    I'm entering Generated On that is "خلقت على" but I'm getting as "ىلع تقلخ", it is getting perfect in java console but in pdf it is getting something other. – Swetha Arvind Jun 09 '17 at 11:15
  • Your question is answered in details in this [post](https://stackoverflow.com/questions/48284888/writing-arabic-characters-with-pdfbox-in-their-correct-presentation-form-without/48346903?noredirect=1#comment83693064_48346903) – Mina Gerges Jan 20 '18 at 21:12
  • @SwethaArvind make the fields editable and use acrofields to update the field values – Danyal Sandeelo Apr 01 '19 at 13:03

0 Answers0