我目前正在使用ItestSharp处理ASP.Net并制作了一个pdf,但问题是在pdf上设置不同的颜色。在下面的代码中,每个新的背景颜色rgb(r,g,b)我必须用bgcolor替换哈希码。 我想从文本传递中搜索RGB颜色文本并转换为哈希码。 搜索是Imp。我想从字符串HtmlText中找到 plzzzzz帮帮我们..
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 :(得分:0)
一种选择是对您的代码执行一些RegEx,但everyone here will tell you that you should not do这样我也不会这样做。相反,我也会告诉你use the HTMLAgilityPack like everyone else。
一旦您引用了该库,就可以使用下面的帮助函数。它将扫描您提供的HTML,查找带有rgb
属性的CSS background-color
函数的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;
}