将Asp.Panel导出为PDF?

时间:2014-01-10 13:20:40

标签: c# asp.net pdf

我想将带有CSS的Asp.Panel content(text, GridViews)导出到C#.NET的PDF。我正在使用带有Asp.Panel的iTextSharp和RenderControl,但CSS不是以PDF格式呈现的。

如何解决此问题(使用iTextSharp(如果可能)或以其他方式)?

此代码生成PDF文件:

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        StringReader sr;
        string fileName = "C://pdf/GridView.pdf";

        var doc = new Document(PageSize.A3, 45, 5, 5, 5);
        var pdf = fileName;


        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdf, FileMode.Create));


        doc.Open();

        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

        cssResolver.AddCssFile(Server.MapPath("Content/PDFs.css"), true);
        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, writer)));

        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser xmlParse = new XMLParser(true, worker);

        this.pnlTabs.RenderControl(htw);
        sr = new StringReader(sw.ToString());
        xmlParse.Parse(sr);
        xmlParse.Flush();
        doc.Close();

这是我想发送给PDF的ASP面板:

     <asp:Panel ID="pnlTabs" runat="server" CssClass="TeamTabs">

        <div class="repHeader">
            <div class="row">
                <div style="padding:12px;border-bottom:1px solid #ddd;margin-bottom:1px;overflow:hidden;">
                    <div class="col4">
                        <p> <font size="4.5"> <b>Client Scorecard</b> </font>  <br>
                            Run Date: 11/1/2013 4:20:01 AM <br>
                            For Dates: 9/12013 - 10/31/2013 <br>
                            Oct 2013 - Filed to Service Complete: 31.18 <br>
                            Oct 2013 - State Average: 34.45 
                        </p>
                    </div>
                </div>
                    <p style="text-align: center;  margin-top:1px"> <font size="3.5"> <b> BECKER POLIAKOFF (CORAL GABLES) </b> 

                        </font> </p>
            </div>
        </div>


        <div class="row" style="padding-bottom:36px;">
            <div class="col9 col-first">
                <asp:GridView ID="gvDashRep_Left_first" runat="server"  ></asp:GridView>
                <asp:GridView ID="gvDashRep_Left_second" runat="server"  ></asp:GridView>
                <asp:GridView ID="gvDashRep_Left_third" runat="server"  ></asp:GridView>
            </div>

            <div class="col3">
                <asp:GridView ID="gvDashRep_Right_first" runat="server"  ></asp:GridView>
                <asp:GridView ID="gvDashRep_Right_second" runat="server"  ></asp:GridView>
                <asp:GridView ID="gvDashRep_Right_third" runat="server"  ></asp:GridView>
            </div>
                <p style="text-align: center;  margin-top:1px"> <font size="2.5"> *For B/W, items with an asterisk indicate that higer number for Octomber 2013 is considered better. </font> </p>
            <div class="col12 col-first">
                <asp:GridView ID="gvComments" runat="server"  ></asp:GridView>
            </div>
        </div>

    </asp:Panel>   

CSS文件 div.row {min-height:1%;宽度:966px;保证金:0自动;溢出:隐藏; } div.col3 {width:219px;向左飘浮; margin-left:30px;}

1 个答案:

答案 0 :(得分:0)

如果要使用带有css的ITextSharp导出为pdf,还有一个额外的下载(XMLWorker)。您可以从here

获取XMLWorker

要将此设置为应用您的CSS,您需要执行以下操作

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    StringReader sr;
    string fileName = Server.MapPath("PATH TO PDF");

    var doc = new Document(PageSize.A3, 45, 5, 5, 5);
    var pdf = fileName;


        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdf,             FileMode.Create));

        doc.Open();

        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

        cssResolver.AddCssFile(Server.MapPath("PATH TO CSS"), true);
        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, writer)));

        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser xmlParse = new XMLParser(true, worker);

        control.RenderControl(htw);
        sr = new StringReader(sw.ToString());
        xmlParse.Parse(sr);
        xmlParse.Flush();

用相关文件路径替换PATH TO PDF(保存文件的loacation)和PATH TO CSS(您的css文件所在的位置)。当我不得不这样做时,css文件必须是一个外部文件(.css)。