Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert my webpage to pdf , for that is it necessary to to build html then convert that html to pdf?

Thanks in Advance.
Posted
Comments
nani1221 28-Sep-12 4:50am    
If i run this code i got an error i.e,

A page can have only one server-side Form tag.
Arrinters1975 20-Aug-15 4:16am    
Does this happen on: this.Page.RenderControl(hw);
If it does then try adding the following in your code behind:

public override void VerifyRenderingInServerForm(Control control) { }

If the problem remains then additionally add the following in your page's source:

<%@ Page ... EnableEventValidation="false" %>

If the issue does not lie in RenderControl method then try another approach, for example try this C# conversion of HTML web page to a PDF file.

You can convert the web page content into PDF using iTextSharp library. Download ITextsharp and add its reference to your project. ITextsharp is a free HTML to PDF Library at http://sourceforge.net/projects/itextsharp/[^]

C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.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);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    this.Page.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
}
 
Share this answer
 
Comments
nani1221 28-Sep-12 4:48am    
If i run this code i got an error i.e,

A page can have only one server-side Form tag.
GOURI SHANKAR CHHOTRAY 26-Oct-13 3:03am    
me also , getting same error
a page can have only one server side form tag
Madhav Gunjal 14-Nov-15 3:17am    
i am also getting the same error
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900