locked
Corrupt file while exporting in PDF RRS feed

  • Question

  • User41602489 posted

    Hi all,

    Im trying to export whole page using following code its works but when i open it. The file exported using below code is corrupted any one have good idea. How to Export without having problem.


    public void exportPDF()
        {

    Response.Clear(); //this clears the Response of any headers or previous output Response.Buffer = true; //make sure that the entire output is rendered simultaneously

            Response.Buffer = true;
            //////Set content type to MS Excel sheet///Use "application/msword" for MS Word doc files///"application/pdf" for PDF files///
            Response.AppendHeader("content-disposition", "attachment; filename=whatever.pdf");
            Response.ContentType = "application/pdf";
            StringWriter stringWriter = new StringWriter(); //System.IO namespace should be used
            
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            //////Render the entire Page control in the HtmlTextWriter object///We can render individual controls also, like a DataGrid to be///exported in custom format (excel, word etc)///
            this.RenderControl(htmlTextWriter);
            
            Response.Write(stringWriter.ToString());
         
            Response.End();

    }

    Thursday, October 22, 2009 6:31 AM

Answers

All replies

  • User1109032460 posted

    Setting the content type makes no difference to how RenderControl works. It still spits out HTML. You actually need to convert the HTML that is generated into a PDF, which involves understanding the PDF file format and (probably) using one of the many excellent PDF developer libraries that are on the market. 

    Thursday, October 22, 2009 6:47 AM
  • User1315229986 posted

    Hi,

    why dont you go for iTextSharp dll its free and easy to impliment

    http://sourceforge.net/projects/itextsharp/

    http://sourceforge.net/projects/itextsharp/files/

    also refer this gridview export to pdf

    http://www.aspsnippets.com/post/2009/03/14/Export-GridView-To-WordExcelPDFCSV-in-ASPNet.aspx

    Thank You

    Deven Sawant

    India(Mumbai)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 22, 2009 6:52 AM
  • User41602489 posted

    Thanks Deven,

     I have download ItextSharp but still your last link is really usefull for me, its working for simple page but i want to export whole page in that case what im getting picture and other some graphics related stuff are not exported well any solutions?

     

    Thanks for your help

    Khem Raj

    Kathmandu,Nepal

    Thursday, October 22, 2009 12:11 PM