0

I've created one pdf document using itextsharp and would like to populate it with data from a html document

string htmlText = htmlcode.ToString();

var styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.TABLE, "border", "2");

var htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), styles);
document.Open();

//here I create font for text

BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250,
                                                             BaseFont.EMBEDDED);
Font font = new Font(baseFont, 8);

//Now I add element to pdf

foreach (IElement t in htmlarraylist)
{
    document.Add((IElement)t);                        
}

//How can I add font for every element from htmlarraylist?

//for paragraph is like this

doc.Add(new Paragraph("țșăî", font));

but how set this font for IElement?

document.Close();
Rob
  • 4,927
  • 12
  • 49
  • 54
Alex
  • 8,908
  • 28
  • 103
  • 157
  • possible duplicate of [Itextsharp set font for IElement](http://stackoverflow.com/questions/11562589/itextsharp-set-font-for-ielement) – Chris Haas Jul 20 '12 at 13:43

1 Answers1

0

The IElement have a Font property you can assign you font to it

Example:

foreach (IElement t in htmlarraylist)
{
        t.Font = font;
        document.Add(t);
}
HatSoft
  • 11,077
  • 3
  • 28
  • 43
  • I want to add BaseFont.CP1250 for diacritics style.LoadTagStyle(HtmlTags.TABLE, HtmlTags.FACE, "times-roman"); not work right – Alex Jul 19 '12 at 08:51
  • @Alex I have corrected the line t.Font = font; in the foreach loop – HatSoft Jul 19 '12 at 08:59
  • 1
    @Alex looks like the IElement does not have property called Font, so please cast t to Element like this ((Element)t).Font = font; hope this helps. Mind you their will be elemenst that dont have Font property in them so you will have to filter out those – HatSoft Jul 19 '12 at 09:17
  • still doesn't work, ((Element)t).Font Cannot resolve symbol 'Font' (( – Alex Jul 19 '12 at 09:24
  • 1
    @Alex the only suggestion I can give is put a break point inside the foreach and find out what are the properties for ((Element)t it might as simple as the "font" instead of "Font" – HatSoft Jul 19 '12 at 09:29