Jump to content
Sign in to follow this  
saya nurul

Export To Pdf File In C#

Recommended Posts

salam,

 

mcm ne kalu nk export gridview ke pdf file?sy telh cuba koding spt dibwh ni tp sy dpt error bila run. mcm ne nk solve mslh ni?

 

harap ada yg boleh membntu..Trima kasih :)

 

   protected void btnExportPDF_Click(object sender, EventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            //DataList1.AllowPaging = false;
            DataList1.DataBind();
            DataList1.RenderControl(hw);
            //DataList1.HeaderRow.Style.Add("width", "15%");
            //DataList1.HeaderRow.Style.Add("font-size", "10px");
            DataList1.Style.Add("text-decoration", "none");
            DataList1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            DataList1.Style.Add("font-size", "8px");
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();

        }

 

 

error yg keluar :

The document has no pages.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.IOException: The document has no pages.

Source Error:



Line 208: pdfDoc.Open(); Line 209: htmlparser.Parse(sr); Line 210: pdfDoc.Close(); Line 211: Response.Write(pdfDoc); Line 212: Response.End();

 

Share this post


Link to post
Share on other sites

Salam... selamat datang ke putera.com sekali lagi...:)

 

Untuk papar code didalam forum, digalakkan mengunakan tag '[ C O D E ] [ / C O D E ]' untuk memudahkan kami membaca code anda....;)

 

Kembali semula pada masalah anda boleh try tengok disini

 

http://www.aspdotnet-suresh.com/2011/04/how-to-export-gridview-data-to-pdf.html

 

Happy programming.....:D

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...