我在使用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();
另外。图像不能以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' />");