使用htmlparser在c#中将html转换为pdf表时不应用背景颜色

时间:2016-02-11 05:21:03

标签: c# pdf pdf-generation html-parsing itextsharp

我需要转换一些html字符串来生成pdf文件,但它不适用任何样式(内联样式)。如果使用 pdtable 没问题,但在我的情况下,我也无法使用 pdftable
我尝试了以下代码,它成功生成了pdf,但没有样式

我的代码是

string resHtml = "<table style='background:red" width='100%' border='1' cellpadding='0' cellspacing='0'><tr><td align='center'>TD2</td><td align='center'>TD1</td></tr>   <tr><td align='center'>TD2</td><td align='center'>TD1</td></tr>     </table><table><tr border='0'><td style='text-align: center;font-size:10px;color:#99999B;'>This is Computer genarated pay slip does not require any signatures</td></tr></table>"; 

iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();

styles.LoadTagStyle("table", "background", "red"); //I found this in stackoverflow but no use

StringReader stringReader = new StringReader(resHtml);

Document Doc = new Document(PageSize.A4, 10f, 10f, 50f, 20f);
HTMLWorker htmlparser = new HTMLWorker(Doc);
PdfWriter.GetInstance(Doc, Response.OutputStream);
Doc.Open();
htmlparser.Open();
htmlparser.Parse(stringReader);
htmlparser.SetStyleSheet(styles);
htmlparser.Close();

Doc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(Doc);
Response.End();

0 个答案:

没有答案