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 7885549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:06:02+00:00 2026-06-03T05:06:02+00:00

I have a new requirement to have a button in my web page, and

  • 0

I have a new requirement to have a button in my web page, and upon clicking, it displays the web page as a PDF. On researching I found that iTextSharp is used for such purpose.
But I have many Telerik and ASP controls in my web page too. Can I get that also in the PDF using iTextSharp?

If yes then can anyone please provide me a basic link to start using iTextSharp as I haven’t came across this tool yet.

According to the answer from @ahaliav I tried with the following code. However, I’m not getting the controls on the pdf.

    protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        MemoryStream msOutput = new MemoryStream();
        TextReader reader = new StringReader(HtmlContent);

        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4, 30, 30, 30, 30);
        HTMLWorker worker = new HTMLWorker(document);
        // step 2:
        // we create a writer that listens to the document
        // and directs a XML-stream to a file
        PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);

        // step 3: we create a worker parse the document

        // step 4: we open document and start the worker on the document
        document.Open();
        worker.StartDocument();

        // step 5: parse the html into the document
        worker.Parse(reader);

        // step 6: close the document and the worker
        worker.EndDocument();
        worker.Close();
        document.Close();
        //Response.Write(document);
        //Response.End();

    }
    protected override void Render(HtmlTextWriter writer)
    {
        // setup a TextWriter to capture the markup
        TextWriter tw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(tw);

        // render the markup into our surrogate TextWriter
        base.Render(htw);

        // get the captured markup as a string
        string pageSource = tw.ToString();

        // render the markup into the output stream verbatim
        writer.Write(pageSource);

        // remove the viewstate field from the captured markup
        HtmlContent = Regex.Replace(pageSource,
            "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" />",
            "", RegexOptions.IgnoreCase);

        // the page source, without the viewstate field, is in viewStateRemoved
        // do what you like with it

    }

Please help .
I have other option also,that is to display the image of the full web page when i click a button.Can anyone guide me to any possible directions for these two issues please.

I finally changed my code as:

        Response.ContentType = "application/msword";
        Response.AddHeader("content-disposition", "attachment;filename=TestPage.doc");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        MemoryStream msOutput = new MemoryStream();

With this i am able to get the msword document containing all my controls ,but i am also getting some unwanted texts,how can i remove that,if anyone encountered the same problem?

  • 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-06-03T05:06:03+00:00Added an answer on June 3, 2026 at 5:06 am

    After some research i did not find any solution for showing the controls in the html this code below works fine but with out showing the controls,
    in the past i used this http://www.winnovative-software.com/ and it works good for html pages including all controlls you can also test it onlie, the “only” problem is it you need to pay for the license

    protected void btnExportToPdf_Click(object sender, EventArgs e)
        {
            renderPDF = true;
        }
    
        protected override void Render(HtmlTextWriter writer)
        {           
            if (renderPDF == true)
            {
                MemoryStream mem = new MemoryStream();
                StreamWriter twr = new StreamWriter(mem);
                HtmlTextWriter myWriter = new HtmlTextWriter(twr);
                base.Render(myWriter);
                myWriter.Flush();
                myWriter.Dispose();
                StreamReader strmRdr = new StreamReader(mem);
                strmRdr.BaseStream.Position = 0;
                string pageContent = strmRdr.ReadToEnd();
                strmRdr.Dispose();
                mem.Dispose();
                writer.Write(pageContent);
                CreatePDFDocument(pageContent);
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                HtmlTextWriter tw = new HtmlTextWriter(new System.IO.StringWriter(sb));
                base.Render(tw);
                // get the captured markup as a string
                string pageSource = tw.ToString();
                //Get the rendered content
                string sContent = sb.ToString();
                //Now output it to the page, if you want
                writer.Write(sContent);
            }
        }
    
        public void CreatePDFDocument(string strHtml)
        {
    
    
            string strHTMLpath = Server.MapPath("MyHTML.html");
            StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8);
            strWriter.Write(strHtml);
            strWriter.Close();
            string strFileName = HttpContext.Current.Server.MapPath("map1.pdf"); //    strFileName    "C:\\Inetpub\\wwwroot\\Test\\map1.pdf" 
            // step 1: creation of a document-object
            Document document = new Document();
            // step 2:
            // we create a writer that listens to the document
            PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
            StringReader se = new StringReader(strHtml);
            TextReader tr = new StreamReader(Server.MapPath("MyHTML.html"));
    
            //add the collection to the document
            document.Open();                        
            /////////
            iTextSharp.text.html.simpleparser.HTMLWorker worker = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
    
            worker.StartDocument();
    
            //// step 5: parse the html into the document
            worker.Parse(tr);
    
            //// step 6: close the document and the worker
            worker.EndDocument();
    
            //worker.Parse(tr); // getting error "Illegal characters in path"
            document.Close();
            ShowPdf(strFileName);
        }
        public void ShowPdf(string strFileName)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
            Response.ContentType = "application/pdf";
            Response.WriteFile(strFileName);
            Response.Flush();
            Response.Clear();
        }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a requirement that has had me skimming the web for quite sometime
I have Button , which on clicking, displays a Dialog . Everything works like
Im new to asp .net mvc3. I have a requirement that i post a
My requirement is quite simple: I have a button that should replace a FragmentA
I have a requirement to generate a pdf document on a button click on
I am relatively new to Android development. My requirement is such that I have
I have a requirement from a user to have a web page where they
I have an existing application (with MySQL DB). I just got a new requirement
I have a requirement to launch .dot files (ms word templates) as new documents
I am kind of new to JavaScript but I have a requirement where I

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.