Reputation: 17
I know that many people may have asked this question before. I've read almost all of them`but it couldn't help me solve my problem. I'm using iText java library to generate a Persian PDF. I'm using the following
how to use PdfWriter.RUN_DIRECTION_RTL
code:
String ruta = txtruta.getText();
String contenido= txtcontenido.getText();
try {
FileOutputStream archivo = new FileOutputStream(ruta+".pdf");
Document doc = new Document(PageSize.A4,50,50,50,50);
PdfWriter.getInstance(doc, archivo);
doc.open();
BaseFont bfComic = BaseFont.createFont("D:\\Font\\B Lotus.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font font = new Font(bfComic, 12,Font.NORMAL);
doc.add(new Paragraph(contenido,font));
doc.close();
JOptionPane.showMessageDialog(null,"ok");
} catch (Exception e) {
System.out.println("Eroor"+e);
}
Output: Problem
Upvotes: 2
Views: 891
Reputation: 17
I succeeded
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser dlg = new JFileChooser();
int option = dlg.showSaveDialog(this);
if(option==JFileChooser.APPROVE_OPTION){
File f = dlg.getSelectedFile();
txtaddress.setText(f.toString());
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String ruta = txtaddress.getText();
String con= content.getText();
try {
FileOutputStream archivo = new FileOutputStream(ruta+".pdf");
Document doc = new Document(PageSize.A4,50,50,50,50);
PdfWriter Writer = PdfWriter.getInstance(doc, archivo);
doc.open();
LanguageProcessor al = new ArabicLigaturizer();
Writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
BaseFont bfComic = BaseFont.createFont("D:\\Font\\titr.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font font = new Font(bfComic, 12,Font.NORMAL);
Paragraph p = new Paragraph(al.process(con),font);
p.setAlignment(Element.ALIGN_RIGHT);
doc.add(p);
doc.close();
JOptionPane.showMessageDialog(null,"Yes");
} catch (Exception e) {
System.out.println("Eroor"+e);
}
}
Upvotes: 0
Reputation: 91
I haven't worked with Persian language. But, I think your problem will be with the font (B Lotus.ttf) you used. In most of times using a registered Unicode font may solve the problem. Try again using a different font.
Also you can RTL a text phrase using following code.
PdfPCell pdfCell = new PdfPCell(new Phrase(contenido, myUnicodePersianFont));
pdfCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
You will find out a similar question here.
Upvotes: 0
Reputation: 1916
Document.add()
doesn't support RTL text. You'll have to use ColumnText.setRunDirection
or PdfPTable.setRunDirection
.
Upvotes: 2