1

I have my PDF file being generated correctly with Chinese characters if I have the font installed in my system font directory. When I actually deploy this to the Azure website, I won't be able to install the font.

I added the font to the project and it is getting deployed, the application finds the path, but iTextSharp does not use it when the PDF is generated.

The current code which works -

FontFactory.Register("c:/windows/fonts/ARIALUNI.TTF");

This does not work but the path is good -

string arialuniTffFont = System.IO.Path.Combine(Server.MapPath("~/bin/fonts/arialuni.ttf"));
FontFactory.Register(arialuniTffFont);

UPDATED:

    private void CreatePDF(IList<string> HTMLData, string fileName, bool rotate)
    {
        //Create PDF document 
        Document doc = new Document(PageSize.A4, 36, 36, 36, 36);
        if (rotate)
        {
            doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
        }

        HTMLWorker parser = new HTMLWorker(doc);

        string fontpath = Server.MapPath("/Fonts/arialuni.ttf");
        FontFactory.Register(fontpath);

        StyleSheet styles = new StyleSheet();
        styles.LoadTagStyle(HtmlTags.TABLE, HtmlTags.SIZE, "6pt");
        styles.LoadTagStyle(HtmlTags.H3, HtmlTags.SIZE, "10pt");
        styles.LoadTagStyle(HtmlTags.H5, HtmlTags.SIZE, "6pt");
        styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
        styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);

        parser.SetStyleSheet(styles);

        PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create));
        doc.Open();

        //Try/Catch removed
        foreach (var s in HTMLData) {
            StringReader reader = new StringReader(s);
            parser.Parse(reader);
            doc.NewPage();
        }
        doc.Close();
    }

The entire routine that does not produce Chinese characters.

Craig
  • 1,205
  • 2
  • 21
  • 54
  • http://stackoverflow.com/questions/5114658/how-can-i-install-custom-fonts-on-windows-azure does that help at all? – BlakeH Mar 28 '14 at 17:52
  • 1
    Not to doubt you but are you 100% certain that the path is correct? `MapPath()` doesn't validate paths, it only converts something that looks relative into something that looks absolute. So this should not cause an exception `if (!File.Exists(arialuniTffFont)) { throw new FileNotFoundException(); }` – Chris Haas Mar 28 '14 at 20:20
  • @ChrisHaas, I am really close on making this work. The font is actually registered, it is just not using it to display the Chinese characters. What do I need to change? – Craig Apr 01 '14 at 20:35
  • 1
    When you register a font I recommend always assigning it an alias manually instead of relying on what's inside of the font. Try do this `FontFactory.Register(fontpath, "Arial Unicode MS");` – Chris Haas Apr 02 '14 at 13:19
  • @ChrisHaas, I used your suggestion and that didn't work. But I did further research into the problem. First I hard coded the path to where the .ttf file was being deployed, that still did not work. To just make sure I was not loosing my mind, I copied the font from the Window/Font folder over to the deploy folder .. and low and behold that worked. I guess the file that I was using was not the actual font file but the pre-installed font file. Thing work as they should now. Thanks for your help. – Craig Apr 02 '14 at 16:17

1 Answers1

0

The file that I was trying to use was the one that was directly downloaded from the internet. I did not realize that it needed to be extracted. Once I figured this out and got the correct file, it worked correctly without any code changes.

Craig
  • 1,205
  • 2
  • 21
  • 54