Tuesday, 27 September 2011

Read Pdf form field and convert to excel using asp.net ,C#

convert Pdf  form field and to excel file using asp.net ,C#


Download itextsharp.dll file


 

using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Reflection;
using Acrobat;
usingSystem.Collections;
usingiTextSharp.text.pdf;
using System.IO;
usingSystem.Data.SqlClient;

private void button3_Click(objectsender, EventArgs e)
{

stringpdfTemplate = "C:/Users/yudi/Desktop/pdftoexcelsunil.pdf";
PdfReaderpdfReader = new PdfReader(pdfTemplate);
AcroFieldsfields = pdfReader.AcroFields;
string fval = fields.GetField("First name");
string lval = fields.GetField("last name");

string address = fields.GetField("address");
string city = fields.GetField("city");

string state = fields.GetField("state");
string email = fields.GetField("email");

SqlConnectioncon = new SqlConnection("Data Source=.;Initial Catalog=gltest;User ID=sunil;Password=sunil");
con.Open();
SqlCommandcmd = new SqlCommand("insert into pdftable(firstname,lastname,address,city,state,email) values('"+fval+"','"+lval+"','"+address+"','"+city+"','"+state+"','"+email+"') ",con);

cmd.ExecuteNonQuery();
con.Close();
int coun=   pdfReader.AcroForm.Fields.Count;

//PdfDocument pdfdoc = new PdfDocument();
byte[] bytes = System.IO.File.ReadAllBytes("C:/Users/yudi/Desktop/pdftoexcelsunil.pdf");

System.IO.File.WriteAllBytes("C:/Users/yudi/Desktop/newpdftoexcelsunil.xls", bytes);

save("newpdftoexcelsunil");


}
public void save(stringfilename)
{
SqlConnectioncon = new SqlConnection("Data Source=.;Initial Catalog=gltest;User ID=sunil;Password=sunil");
con.Open();
//try
{
System.Windows.Forms.SaveFileDialogopendlg = new System.Windows.Forms.SaveFileDialog();
opendlg.FileName = filename;
opendlg.Filter = "Excel Spreadsheets (*.xls)|*.xls";
opendlg.DefaultExt = "xls";
if(opendlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StringBuildersbrHTML = new StringBuilder();
SqlCommand dc = new SqlCommand("select * from pdftable", con);
SqlDataReaderdr;
dr = dc.ExecuteReader();
//Making HTML
sbrHTML.Append("
");
sbrHTML.Append("
");
sbrHTML.Append("
");
for (int i = 0; i < dr.FieldCount; i++)
{
sbrHTML.Append("
");
}
sbrHTML.Append("
");
while(dr.Read())
{
sbrHTML.Append("
");
for (int i = 0; i < dr.FieldCount; i++)
{
sbrHTML.Append("
");
}
sbrHTML.Append("
");
}

dr.Close();
sbrHTML.Append("
sql data
" + dr.GetName(i) + "
" + good_value(dr.GetValue(i).ToString()) + "
");
sbrHTML.Append("

Reported by sunil"
);
sbrHTML.Append("

Date: "
+ System.DateTime.Now + ""
);

//ENDOF MAKING HTML
//WRITING AS AN XSL
StreamWriterswXLS = new StreamWriter(opendlg.FileName);
swXLS.Write(sbrHTML.ToString());
swXLS.Close();
con.Close();
}
}


}
private string good_value(stringinput)
{
return ((input == "" || (input[0] >= '0' && input[0] <= '9')) ? "'": "") + input;
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger