4

I want to display Special Character India Rupee Symbol in iTextPDf,

My Code:

 Font fontRupee = FontFactory.GetFont("Arial", "₹", true, 12);
 Chunk chunkRupee = new Chunk(" ₹ 5410", font3);
2
  • Is Arial font has Indian rupee symbol??
    – Gnanz
    Jul 4, 2013 at 12:08
  • Yes Arial font has Indian rupee symbol Jul 8, 2013 at 7:39

3 Answers 3

7

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.

3
  • In local system its working, after uploading on server its not working Jul 4, 2013 at 15:56
  • 1
    It's not working because your server doesn't have access to c:/windows/fonts/arial.ttf. You need to ship the font program with your web application and adapt the path to that ttf accordingly. Jul 4, 2013 at 16:05
  • nice explanation !, even i want to achieve the same thing but by using MigraDoc , so any idea on how to go about? Nov 7, 2014 at 7:45
1

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.

1
  • Your answer is wrong: (1.) you're storing Unicode characters in your source code. That's bad practice. Don't advise other people to make the same mistake you're making. (2.) you're using the WINANSI encoding for a character that AFAIK doesn't exist in WINANSI. (3.) you're advising somebody to use a Type 3 font. There's been a lot of discussion at the ISO about Type 3 fonts. Nobody likes them; many want to remove Type 3 fonts from the spec, but we can't for backward compatibility. It's better practice to use the Unicode alternative. Jul 4, 2013 at 12:33
0
   public async Task<byte[]> GeneratePdf(string htmlContent, string path)
    {
        try
        {
            APICall aPICall = new APICall();

            StringReader sr = new StringReader(htmlContent);
            Document pdfDoc = new Document(iTextSharp.text.PageSize.A4, 60f, 40f, 80f, 100f);
            StyleSheet styles = new StyleSheet();

            string ttffilepath ="c:/windows/fonts/arial.ttf";
            FontFactory.Register(ttffilepath, "Garamond"); 
            styles.LoadTagStyle("body", "encoding", "Identity-H");
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc, null, styles);
            List<string> signNames = new List<string>();

            using (MemoryStream memoryStream = new MemoryStream())
            {

                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
                pdfDoc.Open();


                htmlparser.SetStyleSheet(styles);
                htmlparser.Parse(sr);
                pdfDoc.Close();

                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();

                return bytes;
            }
        }
        catch (Exception ex)
        {
            return null;
        }

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.