Don't apply CSS when creating PDF using iTextsharp.dll

I am creating a PDF using iTextSharp.dll, but the problem is that I cannot apply this CSS. I have one div:

<div id="personal" class="headerdiv"> Personal Data </div> 

now my .aspx.cs code looks 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(); 
+7
source share
3 answers

Here are a few things going on. First of all, the HTML / CSS parser in iText and iTextSharp is far from complete. They are definitely very powerful, but there are still ways to go. Each version is getting better, so always make sure you are using the latest version.

Secondly, I saw more HTML / CSS actions in an add-on for iText / iTextSharp called XMLWorker, which you can look at. You no longer load styles, you just go through raw HTML / CSS, and it calculates a lot of things. You can see examples here, see the list of supported CSS attributes , download it here (and get two missing files here and here ).

Third, LoadTagStyle designed to load style attributes for HTML tags, not CSS identifiers or classes. You want to use LoadStyle to load by class:

 styles.LoadStyle("<classname>", "<attribute>", "<value>"); 

Unfortunately, this method still does not do what you want it to always do. For example, to change the font size that you would say:

 styles.LoadStyle("headerdiv", "font-size", "60ptx); 

But to make it work, you can only use relative HTML font sizes (1,2, -1, etc.) or PT sizes, and you should use the size property:

 styles.LoadStyle("headerdiv", "size", "60pt"); //or styles.LoadStyle("headerdiv", "size", "2"); 

LoadStyle honestly feels like an afterthought that was only partially completed, and I recommend not actually using it. Instead, I recommend writing style attributes directly on the line 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 CSS points and again why they are working on the new XMLWorker above.

Finally, to use fonts by name, you first need to register them in iTextSharp, they will not look for them:

 iTextSharp.text.FontFactory.Register(@"c:\windows\fonts\cambria.ttc", "Cambria"); 
+12
source

If anyone still has problems with this. The latest version of itextsharp (currently 5.3.2) greatly improves the HTMLWorker processor.

you can get it here: http://sourceforge.net/projects/itextsharp/

+1
source

The correct way to reference the backgroud color is through the HtmlTags class

 styles.LoadTagStyle(HtmlTags.HEADERCELL, HtmlTags.BACKGROUNDCOLOR, "Blue"); 
+1
source

All Articles