How to export data into pdf file [Resolved]

Posted by santoshkumar413-24453 under C# on 1/21/2014 | Points: 10 | Views : 3335 | Status : [Member] | Replies : 8
Hi,


Any one can help me to export data from grid view to pdf file in asp.net.



Thanks,

Santosh




Responses

Posted by: Satyapriyanayak on: 1/22/2014 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
First add the itextsharp.dll file into the application as click solution explorer- Right click on your application file-Add reference-On browse tab search the itextsharp.dll file from your computer-Click ok-Finish. Then you will notice that a Bin folder will be created where you will find itextsharp.dll file present inside that folder.

Program

Default.aspx code


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

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

</div>
<asp:GridView ID="GridView1" runat="server" BackColor="#FFFF99"
Font-Bold="True" ForeColor="Red">
<HeaderStyle BackColor="#66FFFF" Font-Bold="True" ForeColor="#CC3300" />
<AlternatingRowStyle BackColor="#FFCC99" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Export to Pdf" />
</form>
</body>
</html>


Default.aspx.vb code

Imports System.Data
Imports System.Data.SqlClient
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Imports iTextSharp.text.html.simpleparser
Imports System.IO
Imports iTextSharp.text


Partial Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim str As String
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con.Open()
str = "select * from student"
com = New SqlCommand(Str, con)
Dim reader As SqlDataReader
reader = com.ExecuteReader()
GridView1.DataSource = reader
GridView1.DataBind()
con.Close()
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/pdf"

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

Response.Cache.SetCacheability(HttpCacheability.NoCache)

Dim StringWriter1 As New StringWriter()

Dim HtmlTextWriter1 As New HtmlTextWriter(StringWriter1)

GridView1.RenderControl(HtmlTextWriter1)

Dim StringReader1 As New StringReader(StringWriter1.ToString())

Dim newDocument As New Document(PageSize.A4, 7.0F, 7.0F, 7.0F, 7.0F)

Dim HTMLWorker1 As New HTMLWorker(newDocument)

PdfWriter.GetInstance(newDocument, Response.OutputStream)

newDocument.Open()

HTMLWorker1.Parse(StringReader1)

newDocument.Close()

Response.Write(newDocument)

Response.End()

End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub

End Class


If this post helps you mark it as answer
Thanks

santoshkumar413-24453, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/21/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

In asp.net, we don’t have direct feature to export gridview data to PDF. For that reason here I am using third party library ITextSharp.dll reference

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.RenderControl(hw);
gvdetails.HeaderRow.Style.Add("width", "15%");
gvdetails.HeaderRow.Style.Add("font-size", "10px");
gvdetails.Style.Add("text-decoration", "none");
gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdetails.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End()


Please Mark as Answer if you satisfied with the response.

Thank you,
Govind

santoshkumar413-24453, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mattyclown on: 3/19/2014 [Member] Starter | Points: 25

Up
0
Down

You are use OCR sdk to extract text from pdf or you can save pdf to txt file format or you just use some pdf text extracting control.

http://www.rasteredge.com/how-to/vb-net-imaging/pdf-text-extract/


santoshkumar413-24453, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oliver32 on: 4/10/2014 [Member] Starter | Points: 25

Up
0
Down
How about doing that like this: )
Step 1, Load Data Information
private void btnLoad_Click(object sender, EventArgs e)
{
using (OleDbConnection oleDbConnection = new OleDbConnection())
{
oleDbConnection.ConnectionString = this.tbCS.Text;
OleDbCommand oleDbCommand = new OleDbCommand();
oleDbCommand.CommandText = this.tbCT.Text;
oleDbCommand.Connection = oleDbConnection;
using (OleDbDataAdapter da = new OleDbDataAdapter(oleDbCommand))
{
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView.DataSource = dt;
}
}
}

Step 2, Set Export into PDF

private void btnRun_Click(object sender, EventArgs e)
{
Spire.DataExport.PDF.PDFExport PDFExport = new Spire.DataExport.PDF.PDFExport();
PDFExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
PDFExport.DataTable = this.dataGridView.DataSource as DataTable;
PDFExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
PDFExport.SaveToFile("20110223.pdf");

}

The sample codes are from this Article -http://www.e-iceblue.com/Knowledgebase/Spire.DataExport/Program-Guide/How-to-Export-DataTable-to-PDF.html,check it out.

santoshkumar413-24453, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Evanpan on: 11/28/2015 [Member] Starter | Points: 25

Up
0
Down
Hi, matty.
Do you have any suggetsion or recommendation about the ocr components which can work with the following pdf converter?
http://www.pqscan.com/convert-pdf/
Any suggetsion will be appreciated. Thanks in advance.



Best regards,
Pan

santoshkumar413-24453, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/28/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Evanpan, why don't u put ur question as a fresh post in the forum so a so get more visibility (:

--
Thanks & Regards,
RNA Team

santoshkumar413-24453, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lucygrey on: 1/11/2016 [Member] Starter | Points: 25

Up
0
Down
Hey,I am sharing a link you will get the steps there with code,check it:
http://www.c-sharpcorner.com/UploadFile/0c1bb2/export-gridview-to-pdf/



http://www.keenesystems.com/Services/SoftwareDevelopment.aspx

santoshkumar413-24453, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response