1

am able to convert the aspx page to PDF using ITextSharp. But the problem is CSS not getting applied in the exported PDF Document. Anyone please help me.Thanks in advance. Following is my code.

        WebClient client = new WebClient();
        string contents = string.Empty;
        string str = HttpContext.Current.Request.Url.AbsoluteUri;
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(str);
        HtmlNode mainDivnode = doc.DocumentNode;
        contents = mainDivnode.InnerHtml;
        mainDivnode = doc.DocumentNode.SelectSingleNode("//div[@id='divmfgHeader']");
        contents = contents.Replace(mainDivnode.InnerHtml, string.Empty);
        mainDivnode = doc.DocumentNode.SelectSingleNode("//div[@id='divmfgFooter']");
        contents = contents.Replace(mainDivnode.InnerHtml, string.Empty);
        contents = contents.Replace("Images/", HttpContext.Current.Request.PhysicalApplicationPath + @"Images\");

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment=project1.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StyleSheet st = new StyleSheet();
        st.LoadStyle("header", "color", "#FFF");
        Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(Doc);
        PdfWriter.GetInstance(Doc, Response.OutputStream);
        PdfWriter.GetInstance(Doc, new FileStream(Server.MapPath("~") + "/App_Data/HTMLToPDF.pdf", FileMode.Create));

        Doc.Open();

        var interfaceProps = new Dictionary<string, Object>();
        var ih = new ImageHander() { BaseUri = Request.Url.ToString() };
        interfaceProps.Add(HTMLWorker.IMG_PROVIDER, ih);

        foreach (IElement element in HTMLWorker.ParseToList(new StringReader(contents), st))
        {
            Doc.Add(element);
        }
        Doc.Close();
        Response.End(); 
user2865466
  • 57
  • 4
  • 8

0 Answers0