3

I am trying to create a table with cells that consist of arabic text. Unfortunately, the text isn't visible in the PDF.

BaseFont arabicBaseFont = BaseFont.createFont("C:\\comittmment\\trado.ttf", BaseFont.IDENTITY_H,true);
Font arabicFont1 = new Font(arabicBaseFont, 20, Font.BOLD); 
Font arabicFont2 = new Font(arabicBaseFont, 10, Font.NORMAL);

PdfPTable tableArabic = new PdfPTable(13);   
tableArabic.setWidthPercentage(100);
tableArabic.setTotalWidth(new float[]{ 47f,35f,35f,35f,35f,35f,35f,35f,35f,35f,35f,35f,35f});   
tableArabic.setLockedWidth(true);
tableArabic.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

PdfPCell cell1_A = new PdfPCell(new Paragraph("الباقه ",arabicFont2));
cell1_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell1_A.setNoWrap(true);
PdfPCell cell3_A = new PdfPCell(new Paragraph("شهر 1",arabicFont2));
cell3_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell3_A.setNoWrap(true);
PdfPCell cell4_A = new PdfPCell(new Paragraph("شهر 2",arabicFont2));
cell4_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell4_A.setNoWrap(true);
PdfPCell cell5_A = new PdfPCell(new Paragraph("شهر 3",arabicFont2));
cell5_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell5_A.setNoWrap(true);
PdfPCell cell6_A = new PdfPCell(new Paragraph("شهر 4",arabicFont2));
cell6_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell6_A.setNoWrap(true);
PdfPCell cell7_A= new PdfPCell(new Paragraph("شهر 5",arabicFont2));
cell7_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell7_A.setNoWrap(true);
PdfPCell cell8_A = new PdfPCell(new Paragraph("شهر 6",arabicFont2));
cell8_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell8_A.setNoWrap(true);
PdfPCell cell9_A = new PdfPCell(new Paragraph("شهر 7",arabicFont2));
cell9_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell9_A.setNoWrap(true);
PdfPCell cell10_A = new PdfPCell(new Paragraph("شهر 8",arabicFont2));
cell10_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell10_A.setNoWrap(true);
PdfPCell cell11_A = new PdfPCell(new Paragraph("شهر 9",arabicFont2));
cell11_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell11_A.setNoWrap(true);
PdfPCell cell12_A = new PdfPCell(new Paragraph("شهر 10",arabicFont2));
cell12_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell12_A.setNoWrap(true);
PdfPCell cell13_A= new PdfPCell(new Paragraph("شهر 11",arabicFont2));
cell13_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell13_A.setNoWrap(true);
PdfPCell cell14_A = new PdfPCell(new Paragraph("شهر 12",arabicFont2));
cell4_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell14_A.setNoWrap(true);
PdfPCell cell15_A = new PdfPCell(new Paragraph("باقتي الصغيره ",arabicFont2));
cell5_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell15_A.setNoWrap(true);
PdfPCell cell16_A= new PdfPCell(new Paragraph("18",f1));
cell16_A.setNoWrap(true);
PdfPCell cell17_A = new PdfPCell(new Paragraph("16.5",f1));
cell17_A.setNoWrap(true);
PdfPCell cell18_A = new PdfPCell(new Paragraph("15",f1));
cell18_A.setNoWrap(true);
PdfPCell cell19_A= new PdfPCell(new Paragraph("13.5",f1));
cell19_A.setNoWrap(true);
PdfPCell cell20_A = new PdfPCell(new Paragraph("12",f1));
cell20_A.setNoWrap(true);
PdfPCell cell21_A = new PdfPCell(new Paragraph("10.5",f1));
cell21_A.setNoWrap(true);
PdfPCell cell22_A = new PdfPCell(new Paragraph("9",f1));
cell22_A.setNoWrap(true);
PdfPCell cell23_A= new PdfPCell(new Paragraph("7.5",f1));
cell23_A.setNoWrap(true);
PdfPCell cell24_A = new PdfPCell(new Paragraph("6",f1));
cell24_A.setNoWrap(true);
PdfPCell cell25_A = new PdfPCell(new Paragraph("4.5",f1));
cell25_A.setNoWrap(true);
PdfPCell cell26_A = new PdfPCell(new Paragraph("3",f1));
cell26_A.setNoWrap(true);
PdfPCell cell27_A = new PdfPCell(new Paragraph("1.5",f1));
cell27_A.setNoWrap(true);
PdfPCell cell28_A = new PdfPCell(new Phrase("باقتي المتوسطه ",arabicFont2));
cell28_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell28_A.setNoWrap(true);
PdfPCell cell29_A= new PdfPCell(new Paragraph("38",f1));
cell29_A.setNoWrap(true);
PdfPCell cell30_A= new PdfPCell(new Paragraph("34.8",f1));
cell30_A.setNoWrap(true);
PdfPCell cell31_A= new PdfPCell(new Paragraph("31.7",f1));
cell31_A.setNoWrap(true);
PdfPCell cell32_A= new PdfPCell(new Paragraph("28.5",f1));
cell32_A.setNoWrap(true);
PdfPCell cell33_A= new PdfPCell(new Paragraph("25.3",f1));
cell33_A.setNoWrap(true);
PdfPCell cell34_A= new PdfPCell(new Paragraph("22.2",f1));
cell34_A.setNoWrap(true);
PdfPCell cell35_A= new PdfPCell(new Paragraph("19.0",f1));
cell35_A.setNoWrap(true);
PdfPCell cell37_A= new PdfPCell(new Paragraph("15.8",f1));
cell37_A.setNoWrap(true);
PdfPCell cell38_A= new PdfPCell(new Paragraph("12.7",f1));
cell38_A.setNoWrap(true);
PdfPCell cell39_A= new PdfPCell(new Paragraph("9.5",f1));
cell39_A.setNoWrap(true);
PdfPCell cell40_A= new PdfPCell(new Paragraph("6.3",f1));
cell40_A.setNoWrap(true);
PdfPCell cell41_A= new PdfPCell(new Paragraph("3.2",f1));
cell41_A.setNoWrap(true);
PdfPCell cell42_A= new PdfPCell(new Paragraph("باقتي الالكبيره ",arabicFont2));
cell42_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell42_A.setNoWrap(true);
PdfPCell cell43_A= new PdfPCell(new Paragraph("58",f1));
cell43_A.setNoWrap(true);
PdfPCell cell44_A= new PdfPCell(new Paragraph("53.2",f1));
cell44_A.setNoWrap(true);
PdfPCell cell45_A= new PdfPCell(new Paragraph("48.3",f1));
cell45_A.setNoWrap(true);
PdfPCell cell46_A= new PdfPCell(new Paragraph("43.5",f1));
cell46_A.setNoWrap(true);
PdfPCell cell47_A= new PdfPCell(new Paragraph("38.7",f1));
cell47_A.setNoWrap(true);
PdfPCell cell49_A= new PdfPCell(new Paragraph("33.8",f1));
cell49_A.setNoWrap(true);
PdfPCell cell50_A= new PdfPCell(new Paragraph("29.0",f1));
cell50_A.setNoWrap(true);
PdfPCell cell51_A= new PdfPCell(new Paragraph("24.2",f1));
cell51_A.setNoWrap(true);
PdfPCell cell52_A= new PdfPCell(new Paragraph("19.3",f1));
cell52_A.setNoWrap(true);
PdfPCell cell53_A= new PdfPCell(new Paragraph("14.5",f1));
cell53_A.setNoWrap(true);
PdfPCell cell54_A= new PdfPCell(new Paragraph("9.7",f1));
cell54_A.setNoWrap(true);
PdfPCell cell55_A= new PdfPCell(new Paragraph("4.8",f1));
cell55_A.setNoWrap(true);
PdfPCell cell56_A= new PdfPCell(new Paragraph("باقتي الالكبرى ",arabicFont2));
cell56_A.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell56_A.setNoWrap(true);
PdfPCell cell58_A= new PdfPCell(new Paragraph("78",f1));
cell58_A.setNoWrap(true);
PdfPCell cell59_A= new PdfPCell(new Paragraph("71.5",f1));
cell59_A.setNoWrap(true);
PdfPCell cell60_A= new PdfPCell(new Paragraph("65.0",f1));
cell60_A.setNoWrap(true);
PdfPCell cell61_A= new PdfPCell(new Paragraph("58.5",f1));
cell61_A.setNoWrap(true);
   PdfPCell cell62_A= new PdfPCell(new Paragraph("52.0",f1));
   cell62_A.setNoWrap(true);
   PdfPCell cell63_A= new PdfPCell(new Paragraph("45.5",f1));
   cell63_A.setNoWrap(true);
   PdfPCell cell64_A= new PdfPCell(new Paragraph("39.0",f1));
   cell64_A.setNoWrap(true);
   PdfPCell cell65_A= new PdfPCell(new Paragraph("32.5",f1));
   cell65_A.setNoWrap(true);
   PdfPCell cell66_A= new PdfPCell(new Paragraph("26.0",f1));
   cell66_A.setNoWrap(true);
   PdfPCell cell67_A= new PdfPCell(new Paragraph("19.5",f1));
   cell67_A.setNoWrap(true);
   PdfPCell cell68_A= new PdfPCell(new Paragraph("13.0",f1));
   cell68_A.setNoWrap(true);
   PdfPCell cell69_A= new PdfPCell(new Paragraph("6.5",f1));
   cell69_A.setNoWrap(true);


   tableArabic.addCell(cell1_A);
   tableArabic.addCell(cell3_A);
   tableArabic.addCell(cell4_A);
   tableArabic.addCell(cell5_A);
   tableArabic.addCell(cell6_A);
   tableArabic.addCell(cell7_A);
   tableArabic.addCell(cell8_A);
   tableArabic.addCell(cell9_A);
   tableArabic.addCell(cell10_A);
   tableArabic.addCell(cell11_A);
   tableArabic.addCell(cell12_A);
   tableArabic.addCell(cell13_A);
   tableArabic.addCell(cell14_A);
   tableArabic.addCell(cell15_A);
   tableArabic.addCell(cell16_A);
   tableArabic.addCell(cell17_A);
   tableArabic.addCell(cell18_A);
   tableArabic.addCell(cell19_A);
   tableArabic.addCell(cell20_A);
   tableArabic.addCell(cell21_A);
   tableArabic.addCell(cell22_A);
   tableArabic.addCell(cell23_A);
   tableArabic.addCell(cell24_A);
   tableArabic.addCell(cell25_A);
   tableArabic.addCell(cell26_A);
   tableArabic.addCell(cell27_A);
   tableArabic.addCell(cell28_A);
   tableArabic.addCell(cell29_A);
   tableArabic.addCell(cell30_A);
   tableArabic.addCell(cell31_A);
   tableArabic.addCell(cell32_A);
   tableArabic.addCell(cell33_A);
   tableArabic.addCell(cell34_A);
   tableArabic.addCell(cell35_A);
   tableArabic.addCell(cell37_A);
   tableArabic.addCell(cell38_A);
   tableArabic.addCell(cell39_A);
   tableArabic.addCell(cell40_A);
   tableArabic.addCell(cell41_A);
   tableArabic.addCell(cell42_A);
   tableArabic.addCell(cell43_A);
   tableArabic.addCell(cell44_A);
   tableArabic.addCell(cell45_A);
   tableArabic.addCell(cell46_A);
   tableArabic.addCell(cell47_A);

   tableArabic.addCell(cell49_A);
   tableArabic.addCell(cell50_A);
   tableArabic.addCell(cell51_A);
   tableArabic.addCell(cell52_A);
   tableArabic.addCell(cell53_A);
   tableArabic.addCell(cell54_A);
   tableArabic.addCell(cell55_A);
   tableArabic.addCell(cell56_A);
   tableArabic.addCell(cell58_A);
   tableArabic.addCell(cell59_A);
   tableArabic.addCell(cell60_A);
   tableArabic.addCell(cell61_A);
   tableArabic.addCell(cell62_A);
   tableArabic.addCell(cell63_A);
   tableArabic.addCell(cell64_A);
   tableArabic.addCell(cell65_A);
   tableArabic.addCell(cell66_A);
   tableArabic.addCell(cell67_A);
   tableArabic.addCell(cell68_A);
   tableArabic.addCell(cell69_A);

   document.add(tableArabic);

The table is being added to the document but the contents are not let me know where I am going wrong.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Anshuman
  • 51
  • 3
  • You mentioned a stack trace, but there was no stack trace in your question, so I removed that part. If you do have a stack trace, please update your question. – Bruno Lowagie Sep 15 '17 at 10:42
  • No errors are being registered on the console!! I am not aware what's going wrong – Anshuman Sep 15 '17 at 10:46
  • I don't know `trado.ttf`. Replace it with a NOTO font (free fonts by Google) that supports Arabic. Also: you should never use non-Western text in source code. Always use the Unicode value in your code (see the answer by Adam Jaamour). – Bruno Lowagie Sep 15 '17 at 10:49
  • Ensure that you compile (javac -encoding) with the same encoding as you edit (editor). For full Unicode multi-language environments pick UTF-8. – Joop Eggen Sep 15 '17 at 11:06
  • @Anshuman did you manage to solve this? – Adam Jaamour Sep 15 '17 at 16:04
  • No Luck !! can you post the code for creating a table with arabic content for example. – Anshuman Sep 16 '17 at 08:07
  • @Anshuman sorry I didn't see your comment earlier, you forgot to tag me in it – Adam Jaamour Sep 22 '17 at 14:29
  • @Anshuman I edited my answer with sample code from an external source for you to check out how it can be done using UNICODE. Let me know if this helps you solve what you're trying to do. – Adam Jaamour Sep 22 '17 at 14:30

1 Answers1

2

This past answer could help you: How to display Arabic in PDF created using iText

This answer suggests usign unicode values instead of directly typing in arabic in your code as it is bad practice to do so. Make sure that you never use non-Western text in your source code:

Use the UNICODE notation: "\u0627\u0644\u0645\u0648\u0642\u0639 \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a"

Here is an example code from this website for you see how it can be done using UNICODE notation:

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class ArabicTextPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ArabicTextPDF.pdf"));
      document.open();
      java.awt.Font font = new java.awt.Font("arial", 0, 18);
      PdfContentByte cb = writer.getDirectContent();
      java.awt.Graphics2D g2 = cb.createGraphicsShapes(PageSize.A4.width(), PageSize.A4.height());
      g2.setFont(font);
      g2.drawString("\u0634\u0627\u062f\u062c\u0645\u0647\u0648\u0645\u0646", 100, 100);
      g2.dispose();
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}

Also, another solution could be, as suggested, to use arialuni.ttf:

You are assuming that arialbd.ttf can produce Arabic glyphs. As far as I know only arialuni.ttf supports Arabic.

You should note that the latest Windows upgrades don't ship with arialuni.ttf anymore, so as @Bruno Lowagie suggested, you could replace the .ttf file with a NOTO font (free fonts by Google) that supports Arabic. You can learn more about NOTO fonts here.

Let me if if this solves your issue, otherwise I'll try to help you more.

Adam Jaamour
  • 1,326
  • 1
  • 15
  • 31
  • 1
    Indeed; I just upvoted your answer, because that approach has proven to work. Just be aware that the latest Windows upgrades don't ship with `arialuni.ttf` anymore. That's why I proposed to use a NOTO font in my comments. – Bruno Lowagie Sep 15 '17 at 10:51
  • Yes, I agree with you, in that case NOTO font could be a better solution if doesn't have access to `arialuni.ttf` (*which I didn't know wasn't shipped with windows anymore, so thanks for the info). I'll update my answer with your info. – Adam Jaamour Sep 15 '17 at 10:53