asp.net - Itextsharp pdf colore code -
asp.net - Itextsharp pdf colore code -
i working on asp.net itestsharp , made pdf problem setting different color on pdf. in below code every new background-color rgb(r,g,b) have replace bgcolor hash code. want search rgb color text text pass , convert hash code. searching imp. want find 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); }
one alternative perform regex on code everyone here tell you should not do i'm not going either. instead i'll tell use htmlagilitypack else does, too.
once you've got library referenced can utilize below helper function. scan provided html, td tags css rgb
function background-color
property , append converted html attribute. comments in code describe more of what's happening. should relatively easy modify back upwards html tags convert different css properties respective html counterpart.
private static string convertcssbackgroundcolortohtmlbackgroundcolor(string input) { //create 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 value of style attribute , split using css property delimiter var styles = td.getattributevalue("style", "").split(new char[] { ';' }, stringsplitoptions.removeemptyentries); //if there no properties go on next tag if (styles.length == 0) { continue; } //loop through each css property , value foreach (var style in styles) { //split key , value parts ensuring have 2 things. //note: break embedded strings have semicolon won't appear in background color we're safe var parts = style.split(new char[] { ':' }, 2); if (parts.length != 2) { continue; } //grab actual values of key , value, convert lowercase var key = parts[0].trim().tolowerinvariant(); //further, remove whitespace value because can ignore in rgb notation var value = parts[1].trim().tolowerinvariant().replace(" ", ""); //if we're not on color attribute bail if (key != "background-color") { continue; } //if we're not on rgb function bail if (!value.startswith("rgb(") || !value.endswith(")")) { continue; } //grab inner part of rgb function , split @ commas var rgbstrings = value.substring(4, value.length - 5).split(new char[] { ',' }); //sanity check 3 , 3 parts, otherwise bail if (rgbstrings.length != 3) { continue; } //convert values array of ints //note: there should sanity checking on int conversion var rgbints = rgbstrings.select(n => convert.toint32(n)).toarray(); //convert each item in int array hex string notaion , bring together 1 big string var hex = string.join("", rgbints.select(n => n.tostring("x2"))); //finally, set html bgcolor attribute string td.setattributevalue("bgcolor", "#" + hex); } } //return perchance converted html homecoming doc.documentnode.outerhtml; }
asp.net itextsharp
Comments
Post a Comment