将阿拉伯语内容添加到PDF格式的表格中

时间:2017-09-15 10:38:07

标签: java arrays for-loop pdf itext

我正在尝试创建一个包含阿拉伯语文本的单元格的表格。不幸的是,文字在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);

该表正被添加到文档中,但内容不让我知道我哪里出错了。

1 个答案:

答案 0 :(得分:2)

过去的回答可以帮到你: How to display Arabic in PDF created using iText

这个答案建议使用unicode值而不是在代码中直接输入阿拉伯语,因为这样做是不好的做法。确保您从不在源代码中使用非西方文本:

  

使用UNICODE表示法:“\ u0627 \ u0644 \ u0645 \ u0648 \ u0642 \ u0646 \ u0644 \ u0625 \ u0644 \ u062a \ u0632 \ u0648 \ u0646 \ u064a \ u064a”

以下是来自this网站的示例代码,您可以看到如何使用UNICODE表示法完成此操作:

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();
    }
  }
}

另外,正如建议的那样,另一种解决方案可能是使用arialuni.ttf

  

您假设arialbd.ttf可以生成阿拉伯字形。据我所知,只有arialuni.ttf支持阿拉伯语。

您应该注意,最新的Windows升级不再附带arialuni.ttf,因此@Bruno Lowagie建议,您可以用NOTO字体替换.ttf文件(Google提供的免费字体)支持阿拉伯语。您可以了解有关NOTO字体here的更多信息。

如果这可以解决您的问题,请告诉我,否则我会尽力帮助您。