1

Because I use dynamic text, italic or bold text could be any where, cutting everything into chunks is not an option, so I have to use html parser.

Input string is

ąčęėįšųū90-žthis <i>is <b>bold ąčęėįšųū90-ž</i></b> text

String is formatted with iTextSharp html parser:

private Paragraph CreateSimpleHtmlParagraph(String text)
{
    //Our return object
    List<Chunk> c = new List<Chunk>();
    Paragraph p = new Paragraph();
    var styles = new StyleSheet();           
    using (StringReader sr = new StringReader(text))
    {
        //Parse and get a collection of elements
        List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
        foreach (IElement e in elements)
        {
            var chunks = e.Chunks;
            foreach (var chunk in chunks) //list of chunks
            {                        
                chunk.Font.SetFamily(""); //how to set font family here
            }
            p.Add(e);                    
        }
    }
    return p; //getting all the special chars (ąčęėįšųū90-ž...)
}

Code in the main form:

Paragraph pr1 = CreateSimpleHtmlParagraph("ąčęėįšųū90-žthis <i>is <b>bold ąčęėįšųū90-ž</i></b> text");
doc.Add(pr1);

But in PDF I see only

š90-žthis is bold š90-ž text

and no other chars (ąčęėį). I know it has something to do with Fonts, but can't find the problem where. The font should be the same for whole document, times new roman, arial, ect., anything, that could show me my special chars (cp1257, Baltic encoding).

Usually, when I have to format text I use Chunks and my own fonts:

Font arial10n = PdfFontManager.GetFont("c:\\windows\\fonts\\arial.ttf", 10);

colClientTxt.AddText(new Chunk(row["name"].ToString() + "\n", arial10n));

and in PdfFontManager class:

public static Font GetFont(string name, int size)
    {
        BaseFont baseFont = BaseFont.CreateFont(name, BaseFont.CP1257, BaseFont.EMBEDDED);
        Font font = new Font(baseFont, size);
        return font;
    }

So, how to set font family, or maybe there is another way to dinamicaly format my text?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Masius
  • 326
  • 1
  • 6
  • 17
  • 1
    See my post here about registering Unicode fonts for the HTML parser. Specifically, if you do the first three things your code should start outputting a correct PDF. http://stackoverflow.com/a/10336531/231316 – Chris Haas Jul 05 '12 at 16:21

0 Answers0