Click here to Skip to main content
15,874,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to export to pdf in c#.It is successfully happening.
But when table structure is high,the space between the cell is very small and the height of cell increasing and width of the cell decreasing and looks not good.
So i want, if the number of columns is increase ,then the pdf doc will be small and to visible that we have to Zoom it. like some wrap property false of excel.


kindly help me.



using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;




private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}

protected void ExportToPDF(object sender, EventArgs e)
{

string strQuery = "select * from client_master";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);



GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.HeaderRow.Style.Add("background-color", "Blue");
GridView1.AlternatingRowStyle.BackColor = System.Drawing.Color.LightSkyBlue;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.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();
}
Posted

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