Hi Smile,
Please refer below sample.
HTML
<asp:Button Text="Save" runat="server" OnClick="Export" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using iTextSharp.text.pdf;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports System.Text.RegularExpressions
Imports iTextSharp.text.pdf
Code
C#
protected void Export(object sender, EventArgs e)
{
string file = Server.MapPath("~/Q1.pdf");
StringBuilder text = new StringBuilder();
PdfReader pdfReader = new PdfReader(file);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
PdfContentParser parser = new PdfContentParser(new PRTokeniser(pdfReader.GetPageContent(page)));
StringBuilder sb = new StringBuilder();
while (parser.Tokeniser.NextToken())
{
if (parser.Tokeniser.TokenType == PRTokeniser.TK_STRING)
{
sb.Append(parser.Tokeniser.StringValue);
}
}
string currentText = sb.ToString();
currentText = Encoding.UTF8.GetString(Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.UTF8.GetBytes(currentText)));
text.Append(currentText);
pdfReader.Close();
}
DataTable dt = new DataTable();
dt.Columns.Add("Question");
dt.Columns.Add("Option1");
dt.Columns.Add("Option2");
dt.Columns.Add("Option3");
string[] lines = Regex.Split(text.ToString(), @"Q[0-9]{0,3}:");
for (int i = 1; i < lines.Length; i++)
{
if (!string.IsNullOrEmpty(lines[i].Trim()))
{
string[] chars = new string[] { "i)", "ii)", "iii)" };
string[] questionoptions = lines[i].Trim().Split(chars, StringSplitOptions.RemoveEmptyEntries);
dt.Rows.Add(questionoptions[0], questionoptions[1], questionoptions[2], questionoptions[3]);
}
}
for (int i = 0; i < dt.Rows.Count; i++)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO ExamSet(Question, Option1, Option2, Option3) VALUES (@Question, @Option1, @Option2, @Option3)", con))
{
cmd.Parameters.AddWithValue("@Question", dt.Rows[i][0]);
cmd.Parameters.AddWithValue("@Option1", dt.Rows[i][1]);
cmd.Parameters.AddWithValue("@Option2", dt.Rows[i][2]);
cmd.Parameters.AddWithValue("@Option3", dt.Rows[i][3]);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
VB.Net
Protected Sub Export(ByVal sender As Object, ByVal e As EventArgs)
Dim file As String = Server.MapPath("~/Q1.pdf")
Dim text As StringBuilder = New StringBuilder()
Dim pdfReader As PdfReader = New PdfReader(file)
For page As Integer = 1 To pdfReader.NumberOfPages Step 1
Dim parser As PdfContentParser = New PdfContentParser(New PRTokeniser(pdfReader.GetPageContent(page)))
Dim sb As StringBuilder = New StringBuilder()
While parser.Tokeniser.NextToken()
If parser.Tokeniser.TokenType = PRTokeniser.TK_STRING Then
sb.Append(parser.Tokeniser.StringValue)
End If
End While
Dim currentText As String = sb.ToString()
currentText = Encoding.UTF8.GetString(Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.UTF8.GetBytes(currentText)))
text.Append(currentText)
pdfReader.Close()
Next
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Question")
dt.Columns.Add("Option1")
dt.Columns.Add("Option2")
dt.Columns.Add("Option3")
Dim lines As String() = Regex.Split(text.ToString(), "Q[0-9]{0,3}:")
For i As Integer = 1 To lines.Length - 1
If Not String.IsNullOrEmpty(lines(i).Trim()) Then
Dim chars As String() = New String() {"i)", "ii)", "iii)"}
Dim questionoptions As String() = lines(i).Trim().Split(chars, StringSplitOptions.RemoveEmptyEntries)
dt.Rows.Add(questionoptions(0), questionoptions(1), questionoptions(2), questionoptions(3))
End If
Next
For i As Integer = 0 To dt.Rows.Count - 1
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO ExamSet(Question, Option1, Option2, Option3) VALUES (@Question, @Option1, @Option2, @Option3)", con)
cmd.Parameters.AddWithValue("@Question", dt.Rows(i)(0))
cmd.Parameters.AddWithValue("@Option1", dt.Rows(i)(1))
cmd.Parameters.AddWithValue("@Option2", dt.Rows(i)(2))
cmd.Parameters.AddWithValue("@Option3", dt.Rows(i)(3))
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
End Sub
Screenshot