Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying a web page as a pdf with click of a button
    text
    copied!<p>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 <code>iTextSharp</code> is used for such purpose. But I have many <code>Telerik</code> and ASP controls in my web page too. Can I get that also in the PDF using <code>iTextSharp</code>?</p> <p>If yes then can anyone please provide me a basic link to start using <code>iTextSharp</code> as I haven't came across this tool yet.</p> <p>According to the answer from @ahaliav I tried with the following code. However, I'm not getting the controls on the pdf.</p> <pre><code> 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, "&lt;input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" /&gt;", "", RegexOptions.IgnoreCase); // the page source, without the viewstate field, is in viewStateRemoved // do what you like with it } </code></pre> <p>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.</p> <p>I finally changed my code as:</p> <pre><code> Response.ContentType = "application/msword"; Response.AddHeader("content-disposition", "attachment;filename=TestPage.doc"); Response.Cache.SetCacheability(HttpCacheability.NoCache); MemoryStream msOutput = new MemoryStream(); </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload