<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pdf.aspx.cs" Inherits="Pdf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Pdf" /></div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.html;
public partial class Pdf : MyPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
MyPage tmpPage = new MyPage();
HtmlForm form = new HtmlForm();
form.Controls.Add(GridView1);
tmpPage.Controls.Add(form);
StringWriter sw = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
form.Controls[0].RenderControl(htmlWriter);
string htmlContent = sw.ToString();
Document document = new Document();
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.GetInstance(document, new FileStream("c:\\Chap0101.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add a paragraph to the document
//document.Add(new Paragraph(htmlContent.ToString()));
System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(new StringReader(htmlContent));
HtmlParser.Parse(document, _xmlr);
// step 5: we close the document
document.Close();
ShowPdf("c:\\Chap0101.pdf");
}
private void ShowPdf(string s)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + s);
Response.ContentType = "application/pdf";
Response.WriteFile(s);
Response.Flush();
Response.Clear();
}
public DataSet GetData()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Product");
DataRow dr;
dt.Columns.Add(new DataColumn("Price", typeof(Int32)));
dt.Columns.Add(new DataColumn("DisCount", typeof(Int32)));
dt.Columns.Add(new DataColumn("SellPrice", typeof(Int32)));
for (int i = 1; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = i * 2;
dr[2] = 1 * 3;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
Session["dt"] = dt;
return ds;
}
}
*Create a new clas Mypage.cs in app_code folder.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for MyPage
/// </summary>
public class MyPage : Page
{
public override void VerifyRenderingInServerForm(Control control)
{
GridView grid = control as GridView;
if (grid != null && grid.ID == "GridView1")
return;
else
base.VerifyRenderingInServerForm(control);
}
}
Wednesday, July 23, 2008
Export Html to Pdf using iTextSharp(GridView)
Subscribe to:
Post Comments (Atom)


28 comments:
You have a way of explaining things that is easy for me to understand. Not everyone has such a gift
The code I have copied and tried in my system.
But It was showing the following error:
"The type or namespace name 'iTextSharp' could not be found (are you missing a using directive or an assembly reference)"
What to do...Can u help me?
Hi,
Just download iTextsharp lib. from
http://sourceforge.net/projects/itextsharp/
and add reference to your project.
When paging is enable he code wont work, can u help me on that ?
Hey.... I was lookin for somethin like this n quite surprisingly there's not much of it over there...
The tutorials put up are missing the first basic step of integration and tellin you how to do advance stuff without gettin to the basic.
Thanks for this n for puttin it so simply
Man... thanks a lot man.. you are the MAN.... wow... your code works wonders... and i wonder how much you earn with your top notch skills... definitely millions.... good man
hello... if i need to put a WebUserControl, im trying to do this but i getting an error saying thate: Unable to cast object of type 'iTextSharp.text.Paragraph' to type 'iTextSharp.text.Table'. What should i do?
Hi Mauricio
check out therse links
http://forums.asp.net/t/1286980.aspx
http://netpl.blogspot.com/2008/02/unable-to-cast-object-of-type-to-type.html
http://aspdotnetcodebook.blogspot.com/2009/04/how-to-convert-web-page-to-pdf.html
The file generated is corrupt...anything to do?
I love you!
A most excellent article. Thanks. Is there something that can be added to the ShowPDF method to make the resulting pDF appear in a new window instead of in the existing window? Thanks.
thanks dude
Man... i loved it!!! thanks a lot a lot a lot you save my month!
Hi,
I would like first to fill some PDF form fields and then add a table to the same document. Any ideas how this can be done?
Hi
chk this url
http://aspdotnetcodebook.blogspot.com/2009/12/how-to-fill-pdf-form-field-using.html
I know a lot of tools for work with pdf files. But couple days ago I was in unpleasant situation and entered in the Internet,where I noticed - recover pdf. It solved my issue for seconds and for free as I bore in mind.
i have an error named HTMLPARSER Error...
after adding the itextsharp.
can u give a solution
The name 'HtmlParser' does not exist in the current context
This link gives a solution to the HtmlParser:
http://www.dotnetfunda.com/forums/thread2353-solution--htmlparser-doesnt-exsits-in-itextsharp-5020-free-net.aspx
the output's not as nice as converting with Adobe's objects (which I can't get running on a server), but it does work.
Problems seen thus far: script tags, img src's trying to load from C:\
font size is way too big (loss of style info?)
string a = "hello";
MemoryStream MStream = new MemoryStream();
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
try
{
PdfWriter writer = PdfWriter.GetInstance(document, MStream);
document.Open();
document.Add(new iTextSharp.text.Paragraph(strContent));
document.Close();
}
catch (Exception eg)
{
throw eg;
}
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=myPDFNew.pdf");
HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer());
HttpContext.Current.Response.End();
http://www.dotnetfunda.com/forums/thread2353-solution--htmlparser-doesnt-exsits-in-itextsharp-5020-free-net.aspx does indeed give a solution but, I don't understand how to apply it to this article can someone assist ?
Getting compile time error on VerifyRenderingInServerForm as No suitable method found to override
@Rima Gandhi:Inherit your base page class with newly created class MyPage
The above code is good enough to convert a HTML code but i guess you can easily use the new itextShapr library's HTML worker class to make the task even easier. It automatically parses all HML and gives you the PDF. The main loophole is the IMG tag;s relative URLs and it can be taken care by your code. I have written the code and ahred it here (if anybody would like to use it):
http://am22tech.com/s/22/Blogs/post/2011/09/28/HTML-To-PDF-using-iTextSharp.aspx
Font size too small: 0 error :(
public partial class Pdf : MyPage...pls tell me how to call this am new to asp.net
how to create class in asp.net c#
I'm having problems with HtmlParser. VS says that: "The name 'HtmlParser' does not exist in the current context".
Any suggestions?
Currently using itextsharp version 5.3.3.0 and VS 2012.
try this link
http://www.dotnetfunda.com/forums/thread2353-solution-htmlparser-doesnt-exsits-in-itextsharp-5020-free-net.aspx
Post a Comment