Why isn't the Rupee symbol showing?
I tried using arial.ttf and arialbd.ttf, but I don't have any luck with these to show the rupee symbol. Why doesn't it work?
I am using iText for creating PDF. I have read the answer to the question "How to display indian rupee symbol?", but it is not working for me.
This is the code I have used.
BaseFont rupee =BaseFont.createFont("assets/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
createHeadings(cb,495,60,": " + edt_total.getText().toString(),12,rupee);
private void createHeadings(PdfContentByte cb, float x, float y, String text, int size,BaseFont fb){
cb.beginText();
cb.setFontAndSize(fb, size);
cb.setTextMatrix(x,y);
cb.showText(text.trim());
cb.endText();
}
Posted on StackOverflow on Oct 14, 2014
The problem you describe is typical when:
you are using a font which doesn't have that glyph, or
you aren't using the right encoding.
I have written an example that illustrates this: RupeeSymbol.
public static final String DEST = "results/fonts/iea110v7.pdf";
public static final String FONT1 = "src/main/resources/fonts/PlayfairDisplay-Regular.ttf";
public static final String FONT2 = "src/main/resources/fonts/PT_Sans-Web-Regular.ttf";
public static final String FONT3 = "src/main/resources/fonts/FreeSans.ttf";
public static final String RUPEE = "The Rupee character \u20B9 and the Rupee symbol \u20A8";
public void createPdf(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileOutputStream(dest)));
Document doc = new Document(pdfDoc);
PdfFont font1 = PdfFontFactory.createFont(FONT1, PdfEncodings.IDENTITY_H);
PdfFont font2 = PdfFontFactory.createFont(FONT2, PdfEncodings.IDENTITY_H);
PdfFont font3 = PdfFontFactory.createFont(FONT3, PdfEncodings.IDENTITY_H);
PdfFont font4 = PdfFontFactory.createFont(FONT3, PdfEncodings.WINANSI, true);
doc.add(new Paragraph(RUPEE).setFont(font1));
doc.add(new Paragraph(RUPEE).setFont(font2));
doc.add(new Paragraph(RUPEE).setFont(font3));
doc.add(new Paragraph(RUPEE).setFont(font4));
doc.close();
}
The RUPEE
constant is a String
that contains the Rupee character as well as the Rupee symbol: "The Rupee character ? and the Rupee symbol ?"
. The characters are stored as Unicode values, because if we store the characters otherwise, they may not be rendered correctly. For instance: if you retrieve the values from a database as Winansi, you will end up with incorrect characters.
I test three different fonts (PlayfairDisplay-Regular.ttf, PT_Sans-Web-Regular.ttf and FreeSans.ttf) and I use IDENTITY_H
as encoding three times. I also use WINANSI
a fourth time to show that it goes wrong if you do.
The result is a file named rupee.pdf:
Rupee character vs Rupee Symbol
As you can see, the first two fonts know how to draw the Rupee character. The third one doesn't. The first two fonts don't know how to draw the Rupee symbol. The third one does. However, if you use the wrong encoding, none of the fonts draw the correct character or symbol.
In short: you need to find a font that knows how to draw the characters or symbols you need, then you have to make sure that you are using the correct encoding (for the String
as well as the PdfFont
).
Click Why isn't the Rupee symbol showing? if you want to see how to answer this question in iText 5.