-1

I am using following code to convert html to pdf.But arabic is not showing when pdf generated.What is the problem with the following code.

//Create a byte array that will eventually hold our final PDF
Byte[] bytes;


using (var ms = new MemoryStream())
{
    FontFactory.Register(Server.MapPath("~/fonts/TRADBDO.TTF"));
    using (var doc = new Document()) 
    {
        using (var writer = PdfWriter.GetInstance(doc, ms))
        {            
            doc.Open();

            var example_html = @"<p>This <em>is البرامج الدراسية المطروحة البرامج الدراسية المطروحةالبرامج الدراسية المطروحةالبرامج الدراسية المطروحةالبرامج الدراسية المطروحةالبرامج الدراسية المطروحة</em>55555<span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
            var example_css = @".headline{font-size:200%}";

            FontFactory.Register(Server.MapPath("~/fonts/TRADBDO.TTF"));
            using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
            {
                using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
                {
                    iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
                }
            }
            doc.Close();
        }
    }      
    bytes = ms.ToArray();
}

var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test_123_345.pdf");
System.IO.File.WriteAllBytes(testFile, bytes);
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
test user
  • 31
  • 2
  • 9
  • Possible duplicate of [RTL not working in pdf generation with itext 5.5 for Arabic text](http://stackoverflow.com/questions/23889488/rtl-not-working-in-pdf-generation-with-itext-5-5-for-arabic-text). The example is in Java but it's trivial to change it to C#. – Paulo Soares Jan 19 '15 at 09:35
  • 1
    *I am not using itextsharp* - why did you tag it so then? And why are you using iTextSharp classes then? – mkl Jan 19 '15 at 14:42
  • why you negative I am using ITEXT SHARP.Sorry I mistakenly write "not" with my sentence in comment – test user Jan 19 '15 at 16:02

1 Answers1

0

ParseXHtml() takes a parameter that lets you manually handle fonts which is probably the easiest thing to do. If you subclass FontFactoryImp you can then override the GetFont method and specify your own font. The below code does that without much logic and pretty much says "I don't care what font was specified in the HTML, always use this one" which should probably work for you.

public class FontOverrider : FontFactoryImp {
    private readonly BaseFont baseFont;

    /// <summary>
    /// Create a new font factory that always uses the provided font.
    /// </summary>
    /// <param name="fullPathToFontFileToUse">The full path to the font file to use.</param>
    /// <param name="encoding">The type of encoding to use. Default BaseFont.IDENTITY_H. See <see cref="http://api.itextpdf.com/itext/com/itextpdf/text/pdf/BaseFont.html#createFont(java.lang.String, java.lang.String, boolean)"/> for details.</param>
    /// <param name="embedded">Whether or not to embed the entire font. Default True. See <see cref="http://api.itextpdf.com/itext/com/itextpdf/text/pdf/BaseFont.html#createFont(java.lang.String, java.lang.String, boolean)"/> for details.</param>
    public FontOverrider( string fullPathToFontFileToUse, string encoding = BaseFont.IDENTITY_H, bool embedded = BaseFont.EMBEDDED ) {
        //If you are using this class then this font is required and a missing font should be a fatal error
        if (!System.IO.File.Exists(fullPathToFontFileToUse)) {
            throw new System.IO.FileNotFoundException("Could not find the supplied font file", fullPathToFontFileToUse);
        }

        //Create our embedded base font
        baseFont = BaseFont.CreateFont(fullPathToFontFileToUse, encoding, embedded);

    }

    public override iTextSharp.text.Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached) {
        return new iTextSharp.text.Font(baseFont, size, style, color);
    }
}

To use it you just need to change your ParseXHtml() call to this:

iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss, System.Text.Encoding.UTF8, new FontOverrider(myFont));

where myFont is your Server.MapPath() (or whatever) to your full font.

Just a note, I looked online for the font you mentioned and the free version that I found cannot be legally embedded in a PDF. If I try to use it I actually get a message telling me exactly that. This code assumes you've taken care of licensing agreements and have a validly licensed font for embedding. For my sample purposes I just used Arial Unicode MS.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274