iTextSharp导出asp-net面板

本文关键字:面板 asp-net 导出 iTextSharp | 更新日期: 2023-09-27 18:26:49

我下载了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>

请帮帮我,提前谢谢你。

iTextSharp导出asp-net面板

您的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>