Not applying the CSS while generating PDF using iTextsharp.dll
Solution 1
There's a couple of things going on here. First and foremost, the HTML/CSS parser in iText and iTextSharp are far from complete. They're definitely very powerful but still have a ways to go. Each version gets better so always make sure that you're using the latest version.
Second, I've seen more HTML/CSS activity in an add-on for iText/iTextSharp called XMLWorker that you might want to look at. You don't "load styles" anymore, you just pass raw HTML/CSS in and it figures out a lot of things. You can see some examples here, see a list of supported CSS attributes here, download it here (and get the two missing files here and here).
Third, LoadTagStyle
is for loading style attributes for HTML tags, not CSS IDs or Classes. You want to use LoadStyle
to load by class:
styles.LoadStyle("<classname>", "<attribute>", "<value>");
Unfortunately this method still doesn't do what you want it to do always. For instance, to change the font size you'd think you'd say:
styles.LoadStyle("headerdiv", "font-size", "60ptx);
But to get it to work you can only use relative HTML font sizes (1,2,-1, etc) or PT sizes and you must use the size
property:
styles.LoadStyle("headerdiv", "size", "60pt");
//or
styles.LoadStyle("headerdiv", "size", "2");
The LoadStyle
honestly feels like an afterthought that was only partially completed and I recommend not using it actually. Instead I recommend writing the style attributes directly inline if you can:
string html = "<div id=\"personal\" class=\"headerdiv\" style=\"padding-left:50px;font-size:60pt;font-family:Cambria;font-weight:700;\">Personal Data</div>";
Obviously this defeats the points of CSS and once again, that's why they're working on the new XMLWorker above.
Lastly, to use fonts by name you have to register them with iTextSharp first, it won't go looking for them:
iTextSharp.text.FontFactory.Register(@"c:\windows\fonts\cambria.ttc", "Cambria");
Solution 2
In case someone is still having issues with this. The latest version of itextsharp (currently 5.3.2) significantly improves the HTMLWorker processor.
you can get it here: http://sourceforge.net/projects/itextsharp/
Solution 3
The correct way to reference the backgroud color is through the HtmlTags class
styles.LoadTagStyle(HtmlTags.HEADERCELL, HtmlTags.BACKGROUNDCOLOR, "Blue");
Kartik
Updated on July 17, 2020Comments
-
Kartik almost 4 years
I am generating PDF using iTextSharp.dll, but the problem is that I am not able to apply that CSS. I have one div:
<div id="personal" class="headerdiv"> Personal Data </div>
now my .aspx.cs code is like this:
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet(); styles.LoadTagStyle("#headerdiv", "height", "30px"); styles.LoadTagStyle("#headerdiv", "font-weight", "bold"); styles.LoadTagStyle("#headerdiv", "font-family", "Cambria"); styles.LoadTagStyle("#headerdiv", "font-size", "20px"); styles.LoadTagStyle("#headerdiv", "background-color", "Blue"); styles.LoadTagStyle("#headerdiv", "color", "White"); styles.LoadTagStyle("#headerdiv", "padding-left", "5px"); HTMLWorker worker = new HTMLWorker(document); worker.SetStyleSheet(styles); // step 4: we open document and start the worker on the document document.Open(); worker.StartDocument(); // step 5: parse the html into the document worker.Parse(reader); // step 6: close the document and the worker worker.EndDocument(); worker.Close(); document.Close();
-
Kartik over 12 yearsHello this is ok...but i cant apply the background color effect using ur above things...plz give me solution of that...........
-
Chris Haas over 12 yearsUnfortunately the only two way to get background color support is to use tables or to draw shapes on your own
-
Kartik over 12 yearsThnxs Chris Haas sir....i am trying it since last two days.....thnxs for ur useful information.....
-
santhosha over 9 yearscan u please explain me, how to write same thing for applying border to pdf page
-
santhosha over 9 yearsI am trying like this "styles.LoadStyle("pdf", "border", "2px solid #BDBDBD");" but, it is giving "input string was not in correct format" error. How can we write it please tell me.
-
Chris Haas over 9 years@santhosha, please post this as a new question