2017-09-15 136 views
2

我正在尝试使用包含阿拉伯文本的单元格创建表格。不幸的是,该文本在PDF中不可见。将阿拉伯语内容添加到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); 

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

+0

您提到了一个堆栈跟踪,但是在您的问题中没有堆栈跟踪,所以我删除了该部分。如果您确实有堆栈跟踪,请更新您的问题。 –

+0

没有错误正在控制台上注册!我不知道发生了什么问题 – Anshuman

+0

我不知道'trado.ttf'。将其替换为支持阿拉伯语的NOTO字体(由Google免费字体)。另外:你不应该在源代码中使用非西方文本。在代码中始终使用Unicode值(请参阅Adam Jaamour的答案)。 –

回答

2

刚刚过去的这个回答可以帮助你: How to display Arabic in PDF created using iText

这个回答表明替代阿拉伯语在代码中直接输入,因为它是不好的做法,这样做usign Unicode值。请确保你从来不使用非西方文字在源代码:

使用Unicode符号:“\ u0627 \ u0644 \ u0645 \ u0648 \ u0642 \ u0639 \ u0627 \ u0644 \ u0625 \ u0644 \ u0643 \ u062a \ u0631 \ u0648 \ u0646 \ 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(); 
    } 
    } 
} 

而且,是另一个s可以使用arialuni.ttf

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

你应该注意的是最新的Windows升级不arialuni.ttf再出货,从而@Bruno Lowagie建议,你可以替换.ttf文件用能登字体(免费字体由谷歌),支持阿拉伯语。你可以了解更多关于NOTO字体here

如果这能解决您的问题,请联系我,否则我会尽力帮助您。

+1

确实;我刚刚提出了你的答案,因为这种方法已经证明可行。请注意,最新的Windows升级版不再附带'arialuni.ttf'。这就是为什么我建议在我的评论中使用NOTO字体。 –

+0

是的,我同意你的看法,在这种情况下,如果不能访问'arialuni.ttf'(*我不知道它不再与Windows一起发货,那么NOTO字体可能是更好的解决方案,所以感谢信息)。我会用你的信息更新我的答案。 –