使用itextsharp在asp上将datalist导出为PDF时出错

时间:2015-06-06 17:12:30

标签: c# asp.net pdf itextsharp datalist

我使用itextsharp将datalist导出为PDF,但它声明:

  

对象引用未设置为对象的实例

     

描述:执行期间发生了未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:System.NullReferenceException:不是对象引用   设置为对象的实例。

来源错误:

Line 34:             PdfWriter.GetInstance(pdfDoc, Response.OutputStream);<br>
Line 35:             pdfDoc.Open();<br>
Line 36:             htmlparser.Parse(sr); <--- this line<br>
Line 37:             pdfDoc.Close();<br>
Line 38:             Response.Write(pdfDoc);

这里是我的aspx.code

<%@ Page Title="" Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="cetakformulir.aspx.cs" Inherits="Lebah.cetakformulir" %>

    

    <section class="top">
        <div class="wrapper content_header clearfix">
            <div class="work_nav">
            </div>
            <!-- end work_nav -->
            <h1 class="title">Cetak Formulir</h1>
        </div>
    </section>
    <!-- end top -->

    <section class="wrapper">
        <div class="content">
            <asp:DataList ID="DataList2" runat="server" DataKeyField="NIM" DataSourceID="SqlDataSource1">
                <ItemTemplate>
                    <fieldset>
                        <legend></legend>
                    <table>
                        <tr>
                            <td>FORMULIR REGISTRASI KOPMA LEBAH
                            </td>

                        </tr>
                        <tr>
                            <td>
                                <hr />
                            </td>
                            <td>
                                <hr />
                            </td>
                        </tr>
                        <tr>
                            <td>NIM: </td>
                            <td>
                                <asp:Label ID="NIMLabel" runat="server" Text='<%# Eval("NIM") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td>Nama: </td>
                            <td>
                                <asp:Label ID="NamaLabel" runat="server" Text='<%# Eval("Nama") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td>Alamat: </td>
                            <td>
                                <asp:Label ID="AlamatLabel" runat="server" Text='<%# Eval("Alamat") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td>Telepon: </td>
                            <td>
                                <asp:Label ID="TeleponLabel" runat="server" Text='<%# Eval("Telepon") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td>TmptLahir: </td>
                            <td>
                                <asp:Label ID="TmptLahirLabel" runat="server" Text='<%# Eval("TmptLahir") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td>TglLahir: </td>
                            <td>
                                <asp:Label ID="TglLahirLabel" runat="server" Text='<%# Eval("TglLahir") %>' />

                            </td>
                        </tr>
                        <tr>
                            <td>JenisKelamin: </td>
                            <td>
                                <asp:Label ID="JenisKelaminLabel" runat="server" Text='<%# Eval("JenisKelamin") %>' />

                            </td>
                        </tr>
                        <tr>
                            <td>Fakultas: </td>
                            <td>
                                <asp:Label ID="FakultasLabel" runat="server" Text='<%# Eval("Fakultas") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td>Prodi: </td>
                            <td>
                                <asp:Label ID="ProdiLabel" runat="server" Text='<%# Eval("Prodi") %>' />

                            </td>
                        </tr>
                        <tr>
                            <td>
                                <hr />
                            </td>
                            <td>
                                <hr />
                            </td>
                        </tr>
                        <tr >

                            <td colspan="2">
                                <asp:Image ID="img" runat="server" ImageUrl='<%# "imagehandler.ashx?Foto="+Eval("NIM") %>' Height="200px" Width="110px" />
                            </td>
                        </tr>
                        </fieldset>
                    </table>
                </ItemTemplate>
            </asp:DataList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:kopmaConnectionString %>" SelectCommand="SELECT [NIM], [Nama], [Alamat], [Telepon], [TmptLahir], [TglLahir], [JenisKelamin], [Fakultas], [Prodi], [Foto] FROM [datauser] WHERE ([NIM] = @NIM)">
                <SelectParameters>
                    <asp:SessionParameter DefaultValue="" Name="NIM" SessionField="NIM" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:Button ID="Button1" runat="server" Text="Cetak" OnClick="Button1_Click" />
        </div>
        <!-- end content -->
    </section>
</section>

代码背后:

public partial class cetakformulir : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        DataList2.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End(); 
    }
}

}

0 个答案:

没有答案