I have that part of code:
//Staff
// Title font
BaseFont titleBf = null;
try {
titleBf = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.CP1252, BaseFont.EMBEDDED);
} catch (IOException e) {
System.out.println(e.getMessage());
}
com.itextpdf.text.Font titleFont = new com.itextpdf.text.Font(titleBf,24);
titleFont.setColor(new BaseColor(0, 0, 204));
/*
* Pdf creation
*/
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(fc.getSelectedFile() + ".pdf"));
document.open();
/*
* Title
*/
Paragraph p = new Paragraph("مرحبا بكم",titleFont);
p.setSpacingAfter(20);
p.setAlignment(1); // Center
document.add(p);
//Staff
document.close();
The output was empty.
Even when I tried to add simple pdfpcell
, I got same result, here is the way:
com.itextpdf.text.Font fontNormal = FontFactory.getFont(
("arialuni.ttf"), BaseFont.CP1252, BaseFont.EMBEDDED,
8, com.itextpdf.text.Font.NORMAL);
Chunk chunkArabic = new Chunk("مرحبا العالم", fontNormal);
infoTab.addCell(new PdfPCell(new Phrase(chunkArabic)));
document.add(infoTab);
Different problems may be at play.
"\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628"
to avoid problems like this.PdfPCell
and ColumnText
. I see that you're using PdfPCell
, but I don't see you using cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
anywhere. That's wrong.fontNormal
, but you're not telling us which font you're actually using. For instance: if you're using the standard Type1 font Helvetica, no text will show up, because Helvetica doesn't know how to render Arabic text.Amendment: You've now updated your question, showing that you're using "arialuni.ttf"
. However, you are using the wrong encoding: CP1252 is the encoding for the Latin Alphabet. You should use BaseFont.IDENTITY_H
.
Update: looking at the update of your question as well as the extra comment, I see two major errors.
"arialuni.ttf"
. That's the font file, but not the font name. Also: you are asking the FontFactory
for this font, but did you tell the FontFactory
where to look for fonts? Are you sure the FontFactory
can locate c:/windows/fonts/
or whatever directory the file arialuni.ttf
is stored.Paragraph p = new Paragraph("\u062D\u064A\u0633\u0648");
creates a Paragraph
using the default font.This is code that works:
BaseFont bf = BaseFont.createFont(
"c://windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 8);
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("\u062D\u064A\u0633\u0648", font));
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(cell);
document.add(table);
The result looks like this (zoomed in):