For my PDF project, I am taking a string of HTML and using HTMLWorker.ParseToList to add each element to a column. Despite numerous attempts, I have been unable to change the font.
I tried adding the font to each element as I add it: el.Font = times; But unfortunately, this results in an error.
Here is the current code I am working with. My goal is to have all text displayed in Times Roman font.
var specialConditionsText = "<p>Special Conditions</p><p>A. More</p><p>B. Section B</p><p>C. Section C</p><ul><li>Bullet 1</li><li>Bullet 2</li></ul><ol><li>Number list 1</li><li>Number List 2<ol><li>Number list sub</li></ol></li></ol><p><br></p>";
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.UL, HtmlTags.INDENT, "14");
styles.LoadTagStyle(HtmlTags.OL, HtmlTags.INDENT, "14");
styles.LoadTagStyle(HtmlTags.LI, HtmlTags.LEADING, "18");
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font times = new Font(bfTimes, 12, Font.NORMAL);
using (StringReader sr = new StringReader(specialConditionsText))
{
//Get our raw PdfContentByte object letting us draw "above" existing content
PdfContentByte cb = pdfStamper.GetOverContent(11);
//Create a new ColumnText object bound to the above PdfContentByte object
ColumnText ct = new ColumnText(cb);
//Create a single column object spanning the entire page
ct.SetSimpleColumn(60, 190, 570, 720);
//Convert our HTML to iTextSharp elements
List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
//Loop through each element
foreach (IElement el in elements)
{
//Add the element to the ColumnText
el.Font = times;
ct.AddElement(el);
}
ct.Go();
}