Cuando se trata de caracteres Unicode y iTextSharp hay un par de cosas que hay que cuidar. El primero que ya hiciste y eso es obtener una fuente que admita tus personajes. Lo segundo es que realmente desea registrar la fuente con iTextSharp para que esté al tanto de esto.
//Path to our font
string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(arialuniTff);
Ahora que tenemos una fuente que necesitamos para crear un objeto que le dice StyleSheet
iTextSharp cuándo y cómo usarlo.
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
por una parte que no sea HTML que también hay que hacer es establecer un parámetro especial encoding
. Esta codificación es específica de iTextSharp y en su caso desea que sea Identity-H
. Si no configura esto, se configurará por defecto en Cp1252
(WINANSI
).
//Set the default encoding to support Unicode characters
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
Por último, tenemos que pasar nuestra hoja de estilo a la ParseToList
método:
//Parse our HTML using the stylesheet created above
List<IElement> list = HTMLWorker.ParseToList(new StringReader(stringBuilder.ToString()), ST);
poner que todos juntos, de abierto a cerrar tendrías:
doc.Open();
//Sample HTML
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(@"<p>This is a test: <strong>α,β</strong></p>");
//Path to our font
string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(arialuniTff);
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
//Set the default encoding to support Unicode characters
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
//Parse our HTML using the stylesheet created above
List<IElement> list = HTMLWorker.ParseToList(new StringReader(stringBuilder.ToString()), ST);
//Loop through each element, don't bother wrapping in P tags
foreach (var element in list) {
doc.Add(element);
}
doc.Close();
EDITAR
En tu comentario demuestras HTML que especifica una fuente de anulación. iTextSharp no araña el sistema de fuentes y su analizador HTML no utiliza técnicas de recuperación de fuentes. Cualquier fuente especificada en HTML/CSS debe registrarse manualmente.
string lucidaTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "l_10646.ttf");
iTextSharp.text.FontFactory.Register(lucidaTff);
Si el contenido HTML son como