1

I have been able to follow the examples here and have been able to get the Chinese characters to show up if they are outside of a table, but within a cell in a table they wont.

This is the sample html code -

@{
    <head>
    <title>Daily Production Report 国务院公布了房</title>
    </head>
}

@{
    <table style="width: 61%; font-size:x-small; font-family:'Arial Unicode MS'" border="1">
    <tr>
    <td style="width: 78px">Date:</td>
    <td style="width: 200px">&nbsp;</td>
    <td style="width: 80px">Order ID</td>
    <td style="width: 200px">国务院公布了房</td>
    <td style="width: 112px">Total Pieces</td>
    <td style="width: 200px">&nbsp;</td>
    </tr>
    </table>
}

This is the code behind -

FontFactory.Register("c:/windows/fonts/ARIALUNI.TTF");
StyleSheet style = new StyleSheet();
style.LoadTagStyle("body", "face", "Arial Unicode MS");
style.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H);
using (Document document = new Document())
{
    PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));

    document.Open();
    foreach (IElement element in HTMLWorker.ParseToList(
        new StringReader(HTMLData.ToString()), style))
    {
        document.Add(element);
    }
    document.Close();

}
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Craig
  • 1,205
  • 2
  • 21
  • 54

1 Answers1

2

Although your HTML is valid the HTMLWorker's is breaking on the fact that you're enclosing your font name in single quotes within the HTML. Changing font-family:'Arial Unicode MS' to font-family:Arial Unicode MS worked for me. Even better yet, if you control the HTML then just get rid of font-family declaration completely since the <body> tag will just inherit to everything else.

If you don't have the ability to change the generated HTML you can actually rename/re-alias Arial when you declare it, too.

//Register one alias
FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "Arial Unicode MS");
//Register another alias
FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "'Arial Unicode MS'");

Also, please remember that HTMLWorker is long deprecated in favor of XMLWorker.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • that worked great! Thanks... how would I change my code to use XMLWorker? – Craig Mar 26 '14 at 19:30
  • `XMLWorker` is a separate binary, you can pick up the latest here: http://sourceforge.net/projects/itextsharp/files/xmlworker/. It is not a straight upgrade, however. Here are a couple a posts that show examples on using it. http://stackoverflow.com/a/21165654/231316 http://stackoverflow.com/a/16105368/231316 http://stackoverflow.com/a/15362705/231316 – Chris Haas Mar 26 '14 at 20:37