0

I am using iTextSharp.dll to print my page to a pdf. The only issue I am having is that the pdf displays the CSS and JavaScript that I have on the page. Is there a way that I can set iTextSharp to NOT display the CSS/JS?

And yes, I have my CSS/JS typed directly into the .aspx page - as this is a small project. Of course for a large project I would seperate it out.

EDIT
This is my current syntax that writes to the page

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();
Smith Stanley
  • 461
  • 1
  • 8
  • 25
  • Although you didn't share any code, it is clear that you are using the obsolete `HTMLWorker` class. The problem you describe (the fact that the CSS syntax is visible in the PDF file) is an exact duplicate of one of the problems described [here](https://stackoverflow.com/questions/47895935/). – Bruno Lowagie Jan 05 '18 at 09:49
  • @BrunoLowagie - my mistake, I thought I had included my C# syntax. I have updated my post to include such. And impressive you can tell that I am using that class. – Smith Stanley Jan 05 '18 at 13:31
  • That's not so impressive as it was a known problem that was fixed by throwing away `HTMLWorker` and replacing it with something better. – Bruno Lowagie Jan 05 '18 at 15:54

1 Answers1

0

had a similar issue for printing and solved by removing style using string.Replace method look at the code that might help you to solve the problem

  Regex regSimple = new Regex(@"(?i)<img\b[^>]*?style\s*=(['""]?)(?<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=\""));

        }
Ankit
  • 1,094
  • 11
  • 23