iTextSharp导出asp网面板

时间:2015-12-15 14:21:37

标签: c# asp.net pdf itextsharp

我下载了iTextSharp dll的最新版本。

我使用此功能导出PDF c#中的简单标记Panel

<asp:Panel ID="views" runat="server">
    <fieldset style="margin-left: 50px">
        <legend style="font-weight: bold; color: Red; margin-left: 10px;">Testing<br />
            <br />
            1. testing tble</legend>
        <br />
        <table>
            <tr>
                <td>
                    <label for="Zone">
                        Zone<br />
                    </label>
                    <asp:Label ID="Zone" runat="server"></asp:Label></td>
                <td>
                    <label for="cp">
                        cp<br />
                    </label>
                    <asp:Label ID="cp" runat="server"></asp:Label></td>
                <td>
                    <label for="LinesName">
                        LinesName<br />
                    </label>
                    <asp:Label ID="LinesName" runat="server"></asp:Label></td>
                <td>
                    <label for="LinesCode">
                        LinesCode<br />
                    </label>
                    <asp:Label ID="LinesCode" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td>
                    <label for="cl">
                        cl<br />
                    </label>
                    <asp:Label ID="cl" runat="server"></asp:Label></td>
            </tr>
        </table>
    </fieldset>
</asp:Panel>

使用此代码隐藏填充Panel

    using (OdbcCommand command =
            new OdbcCommand(sql, cn))
    {
        try
        {
            command.Parameters.AddWithValue("param1", Decrypt(Request.QueryString["id"].ToString()));
            command.Connection.Open();

            using (OdbcDataAdapter da = 
                new OdbcDataAdapter(command))
            {
                using (OdbcDataReader sdr = command.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        Zone.Text = sdr["Zone"].ToString();
                        cp.Text = sdr["cp"].ToString();
                        LinesName.Text = sdr["LinesName"].ToString();
                        LinesCode.Text = sdr["LinesCode"].ToString();
                        cl.Text = sdr["cl"].ToString();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            command.Connection.Close();
        }

此导出代码PDF

private void PdfFiles1()
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=" + Decrypt(Request.QueryString["id"].ToString()) + ".pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    views.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 30f, 10f, 10f, 10f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

我在PDF返回时没有错误,但我只有table的第一行:

<tr>
    <td>
        <label for="Zone">
            Zone<br />
        </label>
        <asp:Label ID="Zone" runat="server"></asp:Label></td>
    <td>
        <label for="cp">
            cp<br />
        </label>
        <asp:Label ID="cp" runat="server"></asp:Label></td>
    <td>
        <label for="LinesName">
            LinesName<br />
        </label>
        <asp:Label ID="LinesName" runat="server"></asp:Label></td>
    <td>
        <label for="LinesCode">
            LinesCode<br />
        </label>
        <asp:Label ID="LinesCode" runat="server"></asp:Label></td>
</tr>

第二行未打印:

<tr>
    <td>
        <label for="cl">
            cl<br />
        </label>
        <asp:Label ID="cl" runat="server"></asp:Label></td>
</tr>

请帮帮我,非常感谢你。

1 个答案:

答案 0 :(得分:0)

您的HTML表格不平衡,并且iText期望考虑表格中的每个单元格。要修复它,只需给第二行提供适当数量的单元格:

<tr>
    <td colspan="4">
        <label for="cl">
            cl<br />
        </label>
        <asp:Label ID="cl" runat="server"></asp:Label></td>
</tr>

如果你不想跨越它们,你只需要包含空单元格:

<tr>
    <td>
        <label for="cl">
            cl<br />
        </label>
        <asp:Label ID="cl" runat="server"></asp:Label></td>
    <td></td>
    <td></td>
    <td></td>
</tr>

或者:

<tr>
    <td>
        <label for="cl">
            cl<br />
        </label>
        <asp:Label ID="cl" runat="server"></asp:Label></td>
    <td colspan="3"></td>
</tr>