-1

我正在使用面板控制中的一个静态表,如何将面板内的 html 内容转换为 pdf,请在单击按钮导出后立即帮助我转换为 pdf

Aspx 代码:-

<table width="100%">  
   <tr>
      <td>
          <asp:Panel ID="pnlPerson" runat="server">
            <table border="1" style="font-family: Arial; font-size: 10pt; width: 200px">
                <tr>
                    <td colspan="2" style="background-color: #18B5F0; height: 18px; color: White; border: 1px solid white">
                        <b>Personal Details</b>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Name:</b>
                    </td>
                    <td>
                        <asp:Label ID="lblName" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Age:</b>
                    </td>
                    <td>
                        <asp:Label ID="lblAge" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>City:</b>
                    </td>
                    <td>
                        <asp:Label ID="lblCity" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Country:</b>
                    </td>
                    <td>
                        <asp:Label ID="lblCountry" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
          </asp:Panel>
        <asp:Button ID="btnExport" runat="server" Text="Export"  />
      </td>
  </tr>
</table>

C#.net 代码:-

if (!IsPostBack)
{
    //Populate DataTable
    DataTable dt = new DataTable();
    dt.Columns.Add("Name");
    dt.Columns.Add("Age");
    dt.Columns.Add("City");
    dt.Columns.Add("Country");
    dt.Rows.Add();
    dt.Rows[0]["Name"] = "Mudassar Khan";
    dt.Rows[0]["Age"] = "27";
    dt.Rows[0]["City"] = "Mumbai";
    dt.Rows[0]["Country"] = "India";

    //Bind Datatable to Labels
    lblName.Text = dt.Rows[0]["Name"].ToString();
    lblAge.Text = dt.Rows[0]["Age"].ToString();
    lblCity.Text = dt.Rows[0]["City"].ToString();
    lblCountry.Text = dt.Rows[0]["Country"].ToString();
}

protected void btnExport_Click(object sender, EventArgs e)
{
}

请务必向我发送此导出按钮的 C# 代码

4

1 回答 1

0

这就是我所做的,它正在根据需要创建 pdf

protected void btnExport_Click(object sender, EventArgs e)
    {
        var document = new Document(PageSize.A4, 50, 50, 25, 25);
        var output = new FileStream(@"C:\Temp\PersonDetail.pdf", FileMode.Create);
        var writer = PdfWriter.GetInstance(document, output);
        document.Open();


        StringBuilder sb = new StringBuilder();
        StringWriter tw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        pnlPerson.RenderControl(hw);
        var html = sb.ToString();
        var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(html), null);

        // Enumerate the elements, adding each one to the Document...
        foreach (var htmlElement in parsedHtmlElements)
            document.Add(htmlElement as IElement);

        document.Close();

    }

请在http://www.4guysfromrolla.com/articles/030911-1.aspx阅读 Scott Mitchell 的这篇精彩文章,然后决定什么最适合您。正如本文作者所建议的那样,解析 HTML 并将其添加到 PDF 中的支持有限,可能不适用于复杂的 HTML 结构。

让我知道它是否有帮助。

于 2012-10-10T20:17:01.017 回答