iTextSharp XMLWorkerHelper HTML to PDF Table Alignment

4wheeling

New Member
Using the following ASP.net/iTextSharp code to parse the contents of an HTML file into a PDF and dump it into the response stream:\[code\]Response.Clear();Response.ContentType = "application/pdf";using (Document doc = new Document()){ PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream); doc.Open(); using (TextReader reader = File.OpenText(Server.MapPath("~/Test.htm"))) { XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, reader); } doc.Close();}Response.End();\[/code\]This works, but the resulting PDF isn't styled anything like the original HTML page. For starters, the built-in css parser appears to only be able to work with direct tag styles and classes (no chaining like: \[code\]thead th { background-color:#999; }\[/code\]).Second, it appears borders are an all-or-nothing deal. It has no concept of border-top, border-bottom, etc, and border-collapse doesn't collapse the borders of adjoining cells so the borders end up being twice as thick as I want them.Last, I cannot figure out how to align a table to the left- or right-side of the document. It is always centered. I tried wrapping in a div with text-align, tried setting the align attribute, tried setting text-align directly on the table. Can't figure that one out?Here is my demo document I'm trying to use as a proof-of-concept:\[code\]<!DOCTYPE html><html><head> <title>This is the title</title> <meta name="description" content="This is the description" /> <meta name="keywords" content="abc, 123, xyz" /> <style type="text/css"> body { font-family:Arial, Verdana, Sans-Serif; font-size:9pt; } .dataGrid { font-family:Arial, Verdana, Sans-Serif; font-size:9pt; border-collapse: collapse; border:1px solid #000; width:80%; margin:0; text-align:left; } th { padding:3px 4px; font-weight:bold; border:1px solid #000; } td { padding:3px 4px; border:1px solid #000; } .head { border-bottom:2px solid #000; background-color:#9BBA1F; font-weight:bold; } .odd { background-color:#fff; } .even { background-color:#D6EB87; } .foot { border-top:2px solid #000; background-color:#BAB0C4; font-weight:bold; } h1 { font-size:14pt; color:#FFA200; text-align:center; } .right { text-align:right; } .center { text-align:center; } .left { text-align:left; } </style></head><body> <h1>Sample Document</h1> <div style="text-align:left;"> <table class="dataGrid" align="left"> <thead> <tr class="head"> <th width="70%">Name</th> <th width="15%" class="center">Qty</th> <th width="15%" class="center">Price</th> </tr> </thead> <tbody> <tr class="odd"> <td>ABC</td> <td class="center">2</td> <td class="right">$5.00</td> </tr> <tr class="even"> <td>XYZ</td> <td class="center">1</td> <td class="right">$10.00</td> </tr> <tr class="odd"> <td>123</td> <td class="center">3</td> <td class="right">$2.00</td> </tr> <tr class="even"> <td>789</td> <td class="center">1</td> <td class="right">$4.00</td> </tr> </tbody> <tfoot> <tr class="foot"> <td class="right">Totals</td> <td class="center">7</td> <td class="right">$30.00</td> </tr> </tfoot> </table> </div></body></html>\[/code\]
 
Back
Top