iTextSharp Print To PDF 在 PDF 中显示 CSS 和 JS



我正在使用iTextSharp.dll将我的页面打印成pdf。 我遇到的唯一问题是pdf显示了我在页面上的CSS和JavaScript。 有没有办法将iTextSharp设置为显示CSS/JS?

是的,我直接在.aspx页面中键入了我的 CSS/JS - 因为这是一个小项目。 当然,对于一个大型项目,我会将其分开。

编辑
这是我当前写入页面的语法

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(iTextSharp.text.PageSize.A4.Rotate(), 10, 10, 10, 10);
pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4); 
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

打印时遇到了类似的问题,并通过使用字符串删除样式来解决。替换方法查看可能有助于您解决问题的代码

  Regex regSimple = new Regex(@"(?i)<imgb[^>]*?styles*=(['""]?)(?<style>[^'""]+)1[^>]*?>");
        if (regSimple.IsMatch(html))
        {
            html = html.Replace(Convert.ToString(regSimple.Match(html)), Convert.ToString(regSimple.Match(html)).Replace("style="", "").Replace("height:", "height="").Replace(""width:", "width=""));
        }

最新更新