Tuesday, June 4, 2013

How to create pdf from gridview,html?

using System;

using System.Data;

using System.Web.UI.HtmlControls;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using WcfService1;

using System.IO;


using iTextSharp.text;

using iTextSharp.text.pdf;

using iTextSharp.text.html.simpleparser;



 
public partial class testcollapse : System.Web.UI.Page



{
protected void Page_Load(object sender, EventArgs e)



{
if (!IsPostBack)



{

fil();

}

}
public void fil()



{

 
string str = "select top 5 * from tblcustomer";

DataTable dt = new DataTable();

dt = SqlDataAccess.ExecuteDataset(SqlDataAccess.ConnectionString, CommandType.Text, str).Tables[0];



GridView1.DataSource = dt;

GridView1.DataBind();

}
protected void btnsubmit_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);

//Response.End();



 
Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

HtmlForm frm = new HtmlForm();



GridView1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";



frm.Controls.Add(GridView1);

frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);



pdfDoc.Open();

htmlparser.Parse(sr);

pdfDoc.Close();

Response.Write(pdfDoc);

Response.End();

 
/////////////////////

//int columns = GridView1.Columns.Count;

//int rows = GridView1.Rows.Count;

//int tableRows = rows + 3;

//iTextSharp.text.Table gridViewTable = new iTextSharp.text.Table(columns, tableRows);

//gridViewTable.BorderWidth = 1;

//gridViewTable.BorderColor = new Color(0, 0, 255);

//gridViewTable.Cellpadding = 5;

//gridViewTable.Cellspacing = 5;

//Cell c1 = new Cell("Create PDF From GridView Example In Asp.Net");

//c1.Header = true;

//c1.Colspan = 2;

//gridViewTable.AddCell(c1);

//Cell c2 = new Cell("By CsharpAspNetArticles.com");

//c2.Colspan = 2;

//gridViewTable.AddCell(c2);



 
//for (int rowCounter = 0; rowCounter < rows; rowCounter++)

//{

// for (int columnCounter = 0; columnCounter < columns; columnCounter++)

// {

// string strValue = GridView1.Rows[rowCounter].Cells[columnCounter].Text;

// gridViewTable.AddCell(strValue);

// }

//}

//Document Doc = new Document();

//PdfWriter.GetInstance(Doc, Response.OutputStream);

//Doc.Open();

//Doc.Add(gridViewTable);

//Doc.Close();

//Response.ContentType = "application/pdf";

//Response.AddHeader("content-disposition", "attachment; filename=CsharpAspNetArticles.pdf");

//Response.End();



}

}



////////////////////////////
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testcollapse.aspx.cs" Inherits="testcollapse" %>

<!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">

</head>

<body>

<form id="form1" runat="server">




 
<asp:Button ID="btnsubmit" runat="server" Text="submit"

onclick="btnsubmit_Click" />




 
<br /><br />

<asp:GridView ID=GridView1 AutoGenerateColumns=false runat=server>

<Columns>

<asp:TemplateField>

<ItemTemplate>

<span>

<table width="700" ><tr><td><img src = "http://www.aspsnippets.com/images/Blue/Logo.png" /> </td><td></td></tr><tr><td><img src = "http://www.aspsnippets.com/images/Blue/Logo.png" /> </td><td></td></tr></table>

<div style="text-align:justify;">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b> Welcome Letter </b>

<br /><br />

&nbsp;Dear <%# Eval("name") %> <br />

&nbsp;&nbsp;&nbsp;jhnmh hjuy h y h yjh y y jhy jhy y jhnmh hjuy h y h yjh y y jhy jhy yjhnmh hjuy <br />

&nbsp;&nbsp;&nbsp;<img src = "http://www.aspsnippets.com/images/Blue/Logo.png" /> <br />

&nbsp;&nbsp;&nbsp;h y h yjh y y jhy jhy yjhnmh hjuy h y h yjh y y jhy jhy yjhnmh hjuy h y <br />

&nbsp;&nbsp;&nbsp;h y h yjh y y jhy jhy y






 
</div>




 
</span>

</ItemTemplate>




 
</asp:TemplateField>




 
</Columns>

</asp:GridView>




 
</form>

</body>

</html>




Note : Dot use px for width


No comments:

Post a Comment