Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6811147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:17:30+00:00 2026-05-26T20:17:30+00:00

I’m currently trying to export gridview to pdf file. My code is: public void

  • 0

I’m currently trying to export gridview to pdf file.

My code is:

public void GvExportPDF(GridView gvListing, string fileName,int TotalColumns)
{
        http.Response.ContentType = "application/pdf";

        http.Response.AddHeader("content-disposition", "attachment;filename= " + fileName + ".pdf");

        http.Response.Cache.SetCacheability(HttpCacheability.NoCache);

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        gvListing.AllowPaging = false;

        PdfPTable table = new PdfPTable(TotalColumns);

        foreach (GridViewRow rows in gvListing.Rows)
        {
            if (rows.RowType == DataControlRowType.Header)
            {
                for (int i = 0; i < rows.Cells.Count; i++)
                {
                    PdfPCell cell = new PdfPCell();
                    cell.Phrase = new Phrase(getCellText(rows.Cells[i]));
                    table.AddCell(cell);
                }
            }
            else if (rows.RowType == DataControlRowType.DataRow)
            {

                System.Web.UI.WebControls.Image Image1 = (System.Web.UI.WebControls.Image)rows.FindControl("Image1");
                string url = Image1.ImageUrl;
                Image1.Visible = false;

                iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(new Uri(url)); 
                jpg.ScaleToFit(8f, 10f);
                jpg.Border = Rectangle.NO_BORDER;
                for (int i = 0; i < rows.Cells.Count; i++)
                {
                    if (i == 0)
                        table.AddCell(jpg);
                    else
                    {

                        PdfPCell cell = new PdfPCell(new Phrase(HttpContext.Current.Server.HtmlDecode(getCellText(rows.Cells[i]))));


                        table.AddCell(cell);
                    }
                }

            }
        }
        //gvListing.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, http.Response.OutputStream);
        try
        {
            pdfDoc.Open();
            pdfDoc.Add(table);
        }
        catch (DocumentException dex)
        {
            //http.Response.Write(dex.Message);
            System.Diagnostics.Debug.WriteLine(dex.Message);
        }
        catch (IOException ioex)
        {
            //http.Response.Write(ioex.Message);
            System.Diagnostics.Debug.WriteLine(ioex.Message);
        }
        catch (Exception ex)
        {
            //http.Response.Write(ex.Message);
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }

        finally
        {
            pdfDoc.Close();
            //http.Response.Write(StyleOfExportTable());
            http.Response.Output.Write(pdfDoc);
            http.Response.End();
        }
    }

    private string getCellText(TableCell cell)
    {
        StringBuilder sb = new StringBuilder(cell.Text);

        foreach (Control c in cell.Controls)
        {
            if (c.Visible)
            {
                string controlText = getTextProperty(c);
                if (controlText != null)
                {
                    if (sb.Length > 0) sb.Append(" ");
                    sb.Append(controlText);
                }
            }
        }
        return sb.ToString();
    }

    private string getTextProperty(object o)
    {
        PropertyInfo propertyInfo = o.GetType().GetProperty("Text");
        if (propertyInfo != null)
        {
            MethodInfo getMethod = propertyInfo.GetGetMethod();
            if (getMethod != null)
            {
                return (string)getMethod.Invoke(o, null);
            }
        }
        return string.Empty;
    }

It can export pdf and can export the total column I want but besides the image, others cell just display the html code <table><tr><td>text</td></tr></table>.

What is this and how can I solve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:17:31+00:00Added an answer on May 26, 2026 at 8:17 pm

    I don’t have a direct answer but I can tell you with almost certainty that the problem is going to be with whatever you are doing to set the contents of the PdfPCell. The core of iTextSharp has no concept of HTML, almost all commands are PDF-based with a couple of .Net-specific ones in there. There is a parser that turns HTML into PDF-based commands but you aren’t using it. So whatever you are doing to get a cell’s contents is returning HTML which iTextSharp is treating as a string literal. You will either need to have your algorithms return text only or use the iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList to convert the HTML to iTextSharp objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.