Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用itextsharp在asp上将数据列表导出为PDF时出错_C#_Asp.net_Pdf_Itextsharp_Datalist - Fatal编程技术网

C# 使用itextsharp在asp上将数据列表导出为PDF时出错

C# 使用itextsharp在asp上将数据列表导出为PDF时出错,c#,asp.net,pdf,itextsharp,datalist,C#,Asp.net,Pdf,Itextsharp,Datalist,我正在使用itextsharp将数据列表导出为PDF,但它指出: 对象引用未设置为对象的实例 描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源 异常详细信息:System.NullReferenceException:对象引用不存在 设置为对象的实例 源错误: Line 34: PdfWriter.GetInstance(pdfDoc, Response.OutputStream);<br>

我正在使用itextsharp将数据列表导出为PDF,但它指出:

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

描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源

异常详细信息: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);

}

HTMLWorker
已被完全弃用,不受支持。使用
XmlWorker
尝试,如果仍然存在问题,请返回此处。对于某些标记,如

,NRE由
HTMLWorker
抛出。唯一受支持的修复程序是Paulo所说的,切换到
XMLWorker
<%@ 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(); 
    }
}