Hi rani,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Customers with the schema as follows.
You can download the database table SQL by clicking the download link below.
Download SQL file
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Controller
public class HomeController : Controller
{
private IHostingEnvironment Environment;
public HomeController(IHostingEnvironment _environment)
{
Environment = _environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(IFormFile postedFile)
{
string path = System.IO.Path.Combine(this.Environment.WebRootPath, "Uploads");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string fileName = System.IO.Path.GetFileName(postedFile.FileName);
using (FileStream stream = new FileStream(System.IO.Path.Combine(path, fileName), FileMode.Create))
{
postedFile.CopyTo(stream);
}
string currentText = string.Empty;
StringBuilder text = new StringBuilder();
byte[] bytes = System.IO.File.ReadAllBytes(System.IO.Path.Combine(path, fileName));
PdfReader pdfReader = new PdfReader(bytes);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
ITextExtractionStrategy strategy = new LocationTextExtractionStrategy();
currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
currentText = Encoding.UTF8.GetString(Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.UTF8.GetBytes(currentText)));
text.Append(currentText);
pdfReader.Close();
}
string[] data = currentText.Split('\n');
DataTable dt = new DataTable("PdfTable");
string[] headers = data[0].Split(' ');
for (int j = 0; j < headers.Length; j++)
{
if (!string.IsNullOrEmpty(headers[j]))
{
dt.Columns.Add(headers[j], typeof(string));
}
}
for (int i = 1; i < data.Length; i++)
{
if (!string.IsNullOrEmpty(data[i]))
{
string[] content = data[i].Split(' ');
dt.Rows.Add();
for (int k = 0; k < content.Length; k++)
{
dt.Rows[dt.Rows.Count - 1][k] = content[k];
}
}
}
string consString = @"Server=.;DataBase=AjaxSamples;UID=sa;PWD=pass@123";
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
sqlBulkCopy.DestinationTableName = "dbo.Customers";
sqlBulkCopy.ColumnMappings.Add("Id", "CustomerId");
sqlBulkCopy.ColumnMappings.Add("Name", "Name");
sqlBulkCopy.ColumnMappings.Add("Country", "Country");
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
return View();
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
<input type="file" name="postedFile" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
Record after insert