convert Pdf form field and to excel file using asp.net ,C#
Download itextsharp.dll file
");
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Reflection;
using Acrobat;
using
System.Collections;
using
iTextSharp.text.pdf;
using System.IO;
using
System.Data.SqlClient;
private void button3_Click(object
sender, EventArgs e)
{
string
pdfTemplate = "C:/Users/yudi/Desktop/pdftoexcelsunil.pdf";
PdfReader
pdfReader = new PdfReader(pdfTemplate);
AcroFields
fields = 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");
SqlConnection
con = new SqlConnection("Data Source=.;Initial Catalog=gltest;User ID=sunil;Password=sunil");
con.Open();
SqlCommand
cmd = 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(string
filename)
{
SqlConnection
con = new SqlConnection("Data Source=.;Initial Catalog=gltest;User ID=sunil;Password=sunil");
con.Open();
//try
{
System.Windows.Forms.SaveFileDialog
opendlg = 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)
{
StringBuilder
sbrHTML = new StringBuilder();
SqlCommand dc
= new SqlCommand("select * from pdftable", con);
SqlDataReader
dr;
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");
Reported by sunil");
sbrHTML.Append("
Date: " + System.DateTime.Now + "");
Date: " + System.DateTime.Now + "");
//ENDOF MAKING HTML
//WRITING AS AN XSL
StreamWriter
swXLS = new StreamWriter(opendlg.FileName);
swXLS.Write(sbrHTML.ToString());
swXLS.Close();
con.Close();
}
}
}
private string good_value(string
input)
{
return ((input
== "" || (input[0] >= '0' && input[0] <= '9')) ? "'"
: "") + input;
}