4

I am using iText 5.5.10 to create a PDF. When I use English words it works fine, but when I use Arabic words it shows empty lines.

What is the problem, and how can I solve it? Thanks in advance.

public void createPDF1(View view) throws IOException, DocumentException {
    //    BaseFont urName = BaseFont.createFont("assets/subFolder/fontName.TTF", "UTF-8",BaseFont.EMBEDDED);
    //    Font urFontName = new Font(urName, 12);
    Font font = FontFactory.getFont("Simplified Arabic", BaseFont.IDENTITY_H, true, 22, Font.BOLD);

    EditText et=(EditText)findViewById(R.id.txt_input);
    BaseFont ArialBase = BaseFont.createFont("assets/tahoma.ttf", BaseFont.IDENTITY_H, true);
    Font ArialFont = new Font(ArialBase, 20);

    //  Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font chapterFont = FontFactory.getFont("assets/arialuni.ttf", BaseFont.IDENTITY_H, 16, Font.BOLDITALIC);
    Font f=new Font(Font.FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
    Document document = new Document();

    String outpath= Environment.getExternalStorageDirectory()+"/mypdf1.pdf";

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outpath));
    document.open();

    Phrase p = new Phrase("This is incorrect:احمد",ArialFont);
    p.add(new Chunk("احمد",ArialFont));
    p.add(new Chunk(": 50.00 USD"));
    document.add(p);

    p = new Phrase("This is correct: ");
    p.add(new Chunk("#"+et.getText().toString(),chapterFont));
    p.add(new Phrase(": 50.00"));

    ColumnText canvas = new ColumnText(writer.getDirectContent());
    canvas.setSimpleColumn(36, 750, 559, 780);
    canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
    canvas.addElement(p);
    canvas.go();

    document.close();
}

I have tried the suggestions in this post, but it doesn't work.

TofferJ
  • 4,678
  • 1
  • 37
  • 49
sakr dev
  • 49
  • 3
  • Please explain what you mean when you write "It doesn't work." You are adding content three times, and reading your code, this gives an incorrect result in the first attempt, a correct result in the second attempt, and you don't tell us whether the third attempt is correct or incorrect. It's hard to answer a question if you don't explain the exact nature of the problem. Since [this answer was accepted](https://stackoverflow.com/a/31358327/1622493), the best answer that can be given to an "it doesn't work" question is: "it works for us." – Bruno Lowagie Aug 16 '18 at 08:07
  • when i used this code BaseFont ArialBase = BaseFont.createFont("assets/tahoma.ttf", BaseFont.IDENTITY_H, true); Font ArialFont = new Font(ArialBase, 20); the app stop and pdf didn't create . when i deleted this code, the pdf created and arabic word didn't appear – sakr dev Aug 16 '18 at 15:07
  • I found that the problem you encountered was an Assets file inside the res file when deleted this folder. and I clicked right on the app and choose New and then Folder and then Asset Folder then put the font type in it , problem was solved and app work well – sakr dev Aug 16 '18 at 22:42
  • @BrunoLowagie Thanks for your interest and in trying to solve the problem and sorry to waste your time – sakr dev Aug 16 '18 at 22:44

0 Answers0