How to export viewstate data to pdf in ASP.NET C#

BeUnique 2,112 Reputation points
2024-06-18T18:03:07.49+00:00

I am developing asp.net application and i already done from viewstate to excel (not from gridview to excel).

But, i am struggling to export pdf from viewstate.

my viewstate columns is nearly having 20 to 25 columns.

below is my code and correct me where i want to change the code.

also i am getting below error when i used itextsharp.

User's image

private void ExportToPDF()  
{  
  using (DataTable dt = (DataTable)ViewState["FinalData"])
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=Daily_Report.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                //dt.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();
	   }
} 
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,396 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,578 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,821 Reputation points Microsoft Vendor
    2024-06-19T06:56:31.3433333+00:00

    Hi @Gani_tpt,

    You can't "convert" a DataTable to a PDF Document. But you can insert data into it as normal content.

    The specific code is as follows:

     public void ExportToPdf()
     {
         DataTable dt = (DataTable)ViewState["FinalData"];           
         MemoryStream mStream = new MemoryStream();
         Document document = new Document();
         PdfWriter writer = PdfWriter.GetInstance(document,  mStream);
         document.Open();
         PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
         pdfTable.WidthPercentage = 100;
         // Add table headers
         for (int i = 0; i < dt.Columns.Count; i++)
         {
             PdfPCell cell = new PdfPCell(new Phrase(dt.Columns[i].ColumnName));
             cell.BackgroundColor = BaseColor.LIGHT_GRAY;
             pdfTable.AddCell(cell);
         }
         // Add table rows
         for (int row = 0; row < dt.Rows.Count; row++)
         {
             for (int column = 0; column < dt.Columns.Count; column++)
             {
                 pdfTable.AddCell(dt.Rows[row][column].ToString());
             }
         }
         document.Add(pdfTable);
         document.Close();
         Response.ContentType = "application/pdf";
         Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
         Response.Cache.SetCacheability(HttpCacheability.NoCache);
         Response.BinaryWrite(mStream.ToArray());
         Response.End();
     }
    

    HTMLWorker is obsolete

    This is not an error message, just a warning message.

    Obsolete means it is not being used now. But still you can use it.

    You can use XmlWorkerHelper class instead of HtmlWorker to convert HTML string to PDF.

    Run the command below in the Package Manager Console.

    Install-Package iTextSharp.xmlworker

    But this method doesn't apply to your problem, I just add it here.

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful