How to display indian rupee symbol in iText PDF in MVC3

寵の児 提交于 2019-12-01 19:52:38

It's never a good idea to store a Unicode character such as ₹ in your source code. Plenty of things can go wrong if you do so:

  • Somebody can save the file using an encoding different from Unicode, for instance, the double-byte rupee character can be interpreted as two separate bytes representing two different characters.
  • Even if your file is stored correctly, maybe your compiler will read it using the wrong encoding, interpreting the double-byte character as two separate characters.

From your code sample, it's evident that you're not familiar with the concept known as encoding. When creating a Font object, you pass the rupee symbol as encoding.

The correct way to achieve what you want looks like this:

BaseFont bf =
    BaseFont.CreateFont("c:/windows/fonts/arial.ttf",
                        BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
Chunk chunkRupee = new Chunk(" \u20B9 5410", font3);

Note that there are two possible Unicode values for the Rupee symbol (source Wikipedia): \u20B9 is the value you're looking for; the alternative value is \u20A8 (which looks like this: ₨).

I've tested this with arialuni.ttf and arial.ttf. Surprisingly MS Arial Unicode was only able to render ₨; it couldn't render ₹. Plain arial was able to render both symbols. It's very important to check if the font you're using knows how to draw the symbol. If it doesn't, nothing will show up on your page.

Find out which font has indian rupee symbol and import that to iTexy by

BaseFont customfont = BaseFont.createFont(rootpath + "fonts/customfont.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);

Font rupeeFont= new Font(customfont, 9,
                Font.NORMAL, new Color(55, 55, 55));

Chunk chunkRupee = new Chunk("\u20B9", rupeeFont);

Note: While using custom fonts you may need to use some other characters or unicode(U+20B9) for Indian rupee

like Chunk chunkRupee = new Chunk("W", rupeeFont); here in that particular custom font W is for Indian rupee. it depends on that font.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!