我正在尝试创建一个包含由阿拉伯文本组成的单元格的表格。不幸的是,文本在 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);
该表正在添加到文档中,但内容并没有让我知道我哪里出错了。
最佳答案
这个过去的答案可以帮助你:
How to display Arabic in PDF created using iText
这个答案建议使用 unicode 值而不是直接在代码中输入阿拉伯语,因为这样做是不好的做法。确保您永远不会在源代码中使用非西方文本:
这是 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
:您应该注意,最新的 Windows 升级不再附带
arialuni.ttf
,因此正如@Bruno Lowagie 建议的那样,您可以使用支持阿拉伯语的 NOTO 字体(Google 免费字体)替换 .ttf
文件。您可以了解有关 NOTO 字体 here 的更多信息。如果这解决了您的问题,请告诉我,否则我会尽力帮助您。
关于java - 将阿拉伯语内容添加到 PDF 表格中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46237603/