在asp.net c#中使用iTextSharp使用css将aspx页面转换为PDF?

时间:2015-08-19 06:46:32

标签: c# asp.net pdf itextsharp

能够使用ITextSharp将aspx页面转换为PDF。但问题是CSS未在导出的PDF文档中应用。有人请帮帮我。谢谢。以下是我的代码。

        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(); 

0 个答案:

没有答案