2014-06-19 70 views
-1

我目前正在使用ItestSharp與ASP.Net一起製作pdf,但問題是在pdf上設置不同的顏色。在下面的代碼中,每個新的背景色rgb(r,g,b)都必須用bgcolor替換爲散列碼。 我想從文本傳遞中搜索RGB顏色文本並將其轉換爲哈希碼。 搜索是Imp。我想從字符串的htmlText 找到plzzzzz幫我傢伙..Itextsharp pdf colore code

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); 
    } 
+0

儘管你最終使用iTextSharp,你的問題是關於字符串操作和從RGB語法轉換爲HTML十六進制,對不對? –

回答

0

一個選項是在你的代碼,但everyone here will tell you that you should not do執行一些正則表達式說,所以我不打算要麼。相反,我會告訴你use the HTMLAgilityPack like everyone else也是。

一旦你引用了這個庫,你可以使用下面的幫助函數。它將掃描您提供的HTML,查找具有background-color屬性的CSS rgb函數的TD標記並附加轉換後的HTML屬性。代碼中的評論更多地描述了實際發生的事情。對此進行修改應該相對容易,以支持所有HTML標記,並將不同的CSS屬性轉換爲各自的HTML對應項。

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; 
}