-1

I am currently working on ASP.Net with ItestSharp and made a pdf but the problem is for the setting different color on the pdf. In the below code every new background-color rgb(r,g,b) i have to replace with bgcolor with hash code. I want to search the RGB color text from the text pass and convert into hash code. Searching is Imp. I want to find from the string HtmlText plzzzzz help me guys..

 public void OthersTable(Document document, string HtmlText))
        {

            Font fontBold = new Font(f_garamondBold, 11, Font.NORMAL, new Color(0x00, 0x00, 0x00));

            HtmlText = HtmlText.Replace("<p>", "");
            HtmlText = HtmlText.Replace("</p>", "<br>");
            HtmlText = HtmlText.Replace("\"", "'");


            HtmlText = HtmlText.Replace("style='background-color:rgb(191, 191, 191);", " bgcolor='#BFBFBF' style='");
            HtmlText = HtmlText.Replace("style='background-color:rgb(196, 189, 151);", " bgcolor='#C4BD97' style='");
            HtmlText = HtmlText.Replace("style='background-color:rgb(217, 217, 217);", " bgcolor='#D9D9D9' style='");
            HtmlText = HtmlText.Replace("style='background-color:rgb(196,215,155);", "bgcolor='#C4D79B' style='");
            HtmlText = HtmlText.Replace("style='background-color:rgb(230,184,183);", "bgcolor='#E6B8B7' style='");
            HtmlText = HtmlText.Replace("style='background-color:rgb(216,228,188);", "bgcolor='#D8E4BC' style='");
            HtmlText = HtmlText.Replace("style='background-color:rgb(242,220,219);", "bgcolor='#F2DCDB' style='");
            HtmlText = HtmlText.Replace("style='background-color:rgb(227,151,148);", "bgcolor='#E39794' style='");
            HtmlText = HtmlText.Replace("style='background-color:rgb(79, 98, 49);", "bgcolor='#4F6231' style='");
            HtmlText = HtmlText.Replace("style='background-color:rgb(0, 176, 80);", "bgcolor='#00B050' style='");

            HtmlText = "<body>" + HtmlText + "</body>";


            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);




            var parsedHtmlElements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(HtmlText), css);
     }
Vishnu
  • 33
  • 3
  • 13
  • Although you are eventually using iTextSharp, your question is about string manipulation and converting from RGB syntax to HTML hex, right? – Chris Haas Jun 19 '14 at 13:21

1 Answers1

0

One option is to perform some RegEx on your code but everyone here will tell you that you should not do that so I'm not going to either. Instead I'll tell you to use the HTMLAgilityPack like everyone else does, too.

Once you've got that library referenced you can use the below helper function. It will scan your provided HTML, look for TD tags with a CSS rgb function for the background-color property and append the converted HTML attribute. The comments in the code describe more of what's actually happening. It should be relatively easy to modify this to support all HTML tags as well as convert different CSS properties to their respective HTML counterpart.

private static string ConvertCssBackgroundColorToHtmlBackgroundColor(string input) {
    //Create an instance of our Html Agility Pack document
    var doc = new HtmlAgilityPack.HtmlDocument();

    //Load our HTML
    doc.LoadHtml(input);

    //Grab every <td> tag
    foreach (var td in doc.DocumentNode.SelectNodes("//td[@style]")) {

        //Grab the value of the style attribute and split using the CSS property delimiter
        var styles = td.GetAttributeValue("style", "").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

        //If there were no properties then go on to the next tag
        if (styles.Length == 0) {
            continue;
        }

        //Loop through each CSS property and value
        foreach (var style in styles) {
            //Split into key and value parts ensuring that we only have two things.
            //NOTE: This will break with embedded strings that have a semicolon but those won't appear in a background color so we're safe
            var parts = style.Split(new char[] { ':' }, 2);
            if (parts.Length != 2) {
                continue;
            }

            //Grab the actual values of the key and value, convert to lowercase
            var key = parts[0].Trim().ToLowerInvariant();

            //Further, remove all whitespace from the value because that can be ignore in RGB notation
            var value = parts[1].Trim().ToLowerInvariant().Replace(" ", "");

            //If we're not on a color attribute bail
            if (key != "background-color") {
                continue;
            }

            //If we're not on an RGB function then bail
            if (!value.StartsWith("rgb(") || !value.EndsWith(")")) {
                continue;
            }

            //Grab the inner part of the RGB function and split at the commas
            var rgbStrings = value.Substring(4, value.Length - 5).Split(new char[] { ',' });

            //Sanity check for three and only three parts, otherwise bail
            if (rgbStrings.Length != 3) {
                continue;
            }

            //Convert the values into an array of ints
            //NOTE: There probably should be some sanity checking on the int conversion
            var rgbInts = rgbStrings.Select(n => Convert.ToInt32(n)).ToArray();

            //Convert each item in the int array to hex string notaion and join into one big string
            var hex = String.Join("", rgbInts.Select(n => n.ToString("x2")));

            //Finally, set the HTML bgcolor attribute to this string
            td.SetAttributeValue("bgcolor", "#" + hex);
        }
    }

    //Return the possibly converted HTML
    return doc.DocumentNode.OuterHtml;
}
Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274