Exporting HTML and GridView to PDF data showing from 2nd page if more record in ASP.Net

Last Reply 11 months ago By pandeyism

Posted 11 months ago

Exporting html and gridview to pdf showing gridivew data from 2nd page in pdf if data is more

HTML

                        <div id="dvHtml" runat="server" style="margin: 0 0 0 0;">
                            <table style="text-align: left; width: 100%; font-size: 11px; background-color: white" border="0" cellpadding="6" cellspacing="0">
                                <tr>
                                    <td>Report Name :
                                        <asp:Label ID="lblReport" runat="server" Text=""></asp:Label></td>
                                    <td>Report Generated At :
                                        <asp:Label ID="lblReptAt" runat="server" Text=""></asp:Label></td>
                                </tr>
                                <tr>
                                    <td>Report Generated By :
                                        <asp:Label ID="lblReptBy" runat="server" Text=""></asp:Label></td>
                                    <td>Machine IP :
                                        <asp:Label ID="lblReptIP" runat="server" Text=""></asp:Label></td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"
                                            DataKeyNames="theIndex" Width="100%" Font-Size="9">
                                            <Columns>
                                                <asp:BoundField DataField="ackedAlarm" HeaderText="Acknowledged">
                                                    <ItemStyle Width="20px" Height="25px" />
                                                </asp:BoundField>
                                                <asp:BoundField DataField="theDesc" HeaderText="Description">
                                                    <ItemStyle Width="450px" />
                                                </asp:BoundField>
                                                <asp:BoundField DataField="PointLabel" HeaderText="Item Label">
                                                    <ItemStyle Width="50" />
                                                </asp:BoundField>
                                                <asp:BoundField DataField="ackedTime" HeaderText="Action Time">
                                                    <ItemStyle Width="150px" />
                                                </asp:BoundField>
                                                <asp:BoundField DataField="ackedUser" HeaderText="Action User">
                                                    <ItemStyle Width="100px" />
                                                </asp:BoundField>
                                                <asp:BoundField DataField="userText" HeaderText="Action User Text">
                                                    <ItemStyle Width="350px" />
                                                </asp:BoundField>
                                            </Columns>
                                            <HeaderStyle Font-Bold="True" Font-Names="Trebuchet MS"
                                                Font-Size="10" />  
                                        </asp:GridView>
                                    </td>
                                </tr>
                            </table>
                        </div>

C#

        protected void ButnPrint_Click(object sender, EventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename= "+DdlFilter.SelectedItem.Text+" report.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            dvHtml.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            //Document pdfDoc = new Document(PageSize.LEGAL, 5, 5, 5, 5);
            Document pdfDoc = new Document(PageSize.A3, 5f, 5f, 5f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();       
        }
Posted 11 months ago Modified on 11 months ago

Hi ashokpumca,

Please take reference to below code and correct your code.

HTML

<div id="dvHtml" runat="server" style="margin: 0 0 0 0;">  
    <asp:Button ID="btn" runat="server" Text="Print" OnClick="ButnPrint_Click" />      
    <table style="text-align: left; width: 100%; font-size: 11px; background-color: white"
        border="0" cellpadding="6" cellspacing="0">        
        <tr>
            <td>
                Report Name :
                <asp:Label ID="lblReport" runat="server" Text=""></asp:Label>
            </td>
            <td>
                Report Generated At :
                <asp:Label ID="lblReptAt" runat="server" Text=""></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                Report Generated By :
                <asp:Label ID="lblReptBy" runat="server" Text=""></asp:Label>
            </td>
            <td>
                Machine IP :
                <asp:Label ID="lblReptIP" runat="server" Text=""></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" DataKeyNames="CustomerID"
                    Width="100%" Font-Size="9">
                    <Columns>
                        <asp:BoundField DataField="CustomerID" HeaderText="CustomerID">
                            <ItemStyle Width="20px" Height="25px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="CompanyName" HeaderText="CompanyName">
                            <ItemStyle Width="450px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="ContactName" HeaderText="ContactName">
                            <ItemStyle Width="50" />
                        </asp:BoundField>
                        <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle">
                            <ItemStyle Width="150px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Address" HeaderText="Address">
                            <ItemStyle Width="100px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="City" HeaderText="City">
                            <ItemStyle Width="350px" />
                        </asp:BoundField>
                    </Columns>
                    <HeaderStyle Font-Bold="True" Font-Names="Trebuchet MS" Font-Size="10" />
                </asp:GridView>
            </td>
        </tr>
    </table>
</div>

Namespaces

C#

using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Web.UI.WebControls;

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    gvCustomers.DataSource = dt;
                    gvCustomers.DataBind();
                }
            }
        }
    }
}

protected void ButnPrint_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename= report.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    Label label = new Label();
    gvCustomers.Parent.Controls.Add(label);
    label.Controls.Add(gvCustomers);
    dvHtml.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.LEGAL, 5, 5, 5, 5);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}