Click here to Skip to main content
11,712,802 members (81,888 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# PDF
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 2-Jul-12 3:24am
Comments
nani1221 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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/[^]

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);
}
  Permalink  
Comments
nani1221 at 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.
Member 8083093 at 26-Oct-13 3:03am
   
me also , getting same error
a page can have only one server side form tag
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  

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

  Print Answers RSS
0 CHill60 335
1 Sergey Alexandrovich Kryukov 285
2 OriginalGriff 200
3 Maciej Los 145
4 Andy Lanng 140
0 CHill60 335
1 Sergey Alexandrovich Kryukov 320
2 OriginalGriff 225
3 Andy Lanng 165
4 Richard MacCutchan 150


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 2 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100