Asp.net CSS样式未应用于带有iTextSharp的PDF

Asp.net CSS样式未应用于带有iTextSharp的PDF,asp.net,css,pdf,itextsharp,Asp.net,Css,Pdf,Itextsharp,我在使用itextsharp生成Pdf时遇到问题 这是我的密码。请给出解决方案。。我的pdf已成功生成,但缺少样式。生成的html正在浏览器中以样式生成。隐藏字段值hdnTableval.value包含要应用样式的数据表 StringBuilder message = new StringBuilder(); string css=@"<style type='TEXT/CSS'>TABLE{BORDER: 1PX SOLID #DDD

我在使用itextsharp生成Pdf时遇到问题

这是我的密码。请给出解决方案。。我的pdf已成功生成,但缺少样式。生成的html正在浏览器中以样式生成。隐藏字段值hdnTableval.value包含要应用样式的数据表

            StringBuilder message = new StringBuilder();
            string css=@"<style type='TEXT/CSS'>TABLE{BORDER: 1PX SOLID #DDDDDD;MARGIN: 0;PADDING: 5;WIDTH: 100%;}TH{FONT-SIZE:      13PX;COLOR: #999;BORDER-BOTTOM: 1PX SOLID #DDDDDD;BORDER-RIGHT: NONE;BORDER-LEFT: NONE;TEXT-ALIGN: CENTER;PADDING-TOP: 8PX;PADDING-BOTTOM: 8PX;FONT-WEIGHT: BOLD;} TD {FONT-SIZE: 13PX;COLOR: #000;BORDER-BOTTOM: 1PX SOLID #DDDDDD;BORDER-RIGHT: NONE;BORDER-LEFT: NONE;TEXT-ALIGN: CENTER;PADDING-TOP: 8PX;PADDING-BOTTOM: 8PX;}TD.TDSEL{BACKGROUND: #FFFF99;}TH{BACKGROUND: #CECECE;}</style>";
            message.Append("<HTML><head>"+css);


            message.Append(@"</head><BODY>");

            message.Append(hdnTableval.Value.Trim());
            message.Append("</BODY></HTML>");

            Document document = new Document();
            var ms = new MemoryStream();
            var writer = PdfWriter.GetInstance(document, ms);
            writer.CloseStream = false;

            document.Open();

            string htmlText = message.ToString(); // here give HTML

            XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, new StringReader(htmlText));

            document.Close();

            byte[] stream = ms.ToArray();

            HttpContext.Current.Response.Buffer = false;
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            HttpContext.Current.Response.ContentType = "Application/pdf";
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            //Write the file content directly to the HTTP content output stream.

            HttpContext.Current.Response.BinaryWrite(stream);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
StringBuilder消息=新建StringBuilder();
字符串css=@“表格{BORDER:1PX SOLID#ddddddd;边距:0;PADDING:5;宽度:100%;}TH{FONT-SIZE:13PX;颜色:999;BORDER-BOTTOM:1PX SOLID#ddddddddddd;BORDER-RIGHT:NONE;BORDER-LEFT:NONE;TEXT-ALIGN:CENTER;PADDING-TOP:8PX;PADDING-BOTTOM:8PX;FONT-WEIGHT:BOLD;}TD{字体大小:13PX;颜色:#000;边框底部:1PX实心#ddddddd;边框右侧:无;边框左侧:无;文本对齐:中心;填充顶部:8PX;填充底部:8PX;}TD.TDSEL{BACKGROUND:#FFFF99;}TH{BACKGROUND:#CECECE;}”;
message.Append(““+css”);
message.Append(@“”);
Append(hdnTableval.Value.Trim());
消息。追加(“”);
文档=新文档();
var ms=新内存流();
var writer=PdfWriter.GetInstance(文档,ms);
writer.CloseStream=false;
document.Open();
字符串htmlText=message.ToString();//这里给出HTML
XMLWorkerHelper.GetInstance().ParseXHtml(编写器、文档、新StringReader(htmlText));
document.Close();
字节[]流=ms.ToArray();
HttpContext.Current.Response.Buffer=false;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader(“内容处置”,“附件;文件名=TestPage.pdf”);
HttpContext.Current.Response.ContentType=“Application/pdf”;
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
//将文件内容直接写入HTTP内容输出流。
HttpContext.Current.Response.BinaryWrite(流);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
另外,图像不能准确地以pdf格式显示。即使我给出了较大的宽度,它也只能显示在一个小的方形框中

                    string imgPath = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath + "img/" + image;
                head.Append(@"<img height='100' width='703' style='margin:0 auto; display:block;' src='" + imgPath + "' alt='Header Missing' height='42' width='42' />");
string imgPath=Request.Url.GetLeftPart(UriPartial.Authority)+Request.ApplicationPath+“img/”+image;
标题.附加(@“);

您使用的是哪个版本的iTextSharp?以及“hdnTableval”的内容是什么?itext sharp v2.0.50727。并且hdnTableval包含一个带有数据样式的Html表的问题得到了解决。我将粗体CSS改为普通CSS。这是唯一的区别。但图像仍然很小。