Quantcast
Channel: htmlagilitypack Forum Rss Feed
Viewing all articles
Browse latest Browse all 655

New Post: HtmlAgilitypack on strings C#

$
0
0
It's no problem.. What you should do is create a function that will loop through all incidents and and set v as report description
foreach (var incident in incidents){
 
 DrawDescription(incident.Description);

}
then create a method out of main that takes description as a param and load into the document

doc.LoadHtml(description)

it should work assuming all descriptions are similar

i.e have spans containing all the text with italics as tags and bold in the style. If there are some differences you will have to code them in by creating new link if statements

if (link.Attributes["style"].Value.Contains("italic"))
        public static void CreateDocument()
        {
            foreach (var incident in incidents)
            {
                DrawDescription(incident.Description);
            }
        }

        public static void DrawDescription(string description)
        {
            
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(description);

            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//span"))
            {
                bool hasBold = false;
                bool hasItalic = false;
                string text = link.InnerText;
                if (link.Attributes["style"].Value.Contains("bold"))
                {
                    hasBold = true;
                }
                if (link.InnerHtml.Contains("<i>"))
                {
                    hasItalic = true;
                }

                font2 = GetFont(hasBold, hasItalic);

                graphics.DrawString("" + text, font2, XBrushes.Black, new XRect(margin + 90, page.Height - (lineHeight * 35), page.Width, page.Height), XStringFormats.TopLeft);

            }
        }

        private static void GetFont(bool hasBold, bool hasItalic)
        {
            XFont font;
            if (hasBold && hasItalic)
                font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
            else if (hasBold)
                font = new XFont("Verdana", 20, XFontStyle.Bold);
            else if (hasItalic)
                font = new XFont("Verdana", 20, XFontStyle.Italic);
            else
                font = new XFont("Verdana", 20, XFontStyle.Normal);
            return font;
        }

Viewing all articles
Browse latest Browse all 655

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>