Hi smile,
Please refer below sample.
HTML
<asp:GridView ID="gvQuestionBank" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="QuestionCategory">
<ItemTemplate>
<asp:HiddenField ID="hfValue" Value='<%#Eval("QType") %>' runat="server" />
<asp:Label ID="lblCategory" Text='<%#Eval("QuestionCategory") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No Of Question">
<ItemTemplate>
<asp:TextBox ID="txtNo" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnGenerate" OnClick="Generate" Text="Generate" runat="server" />
Namespaces
C#
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Linq;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("QuestionCategory", typeof(string)),
new DataColumn("QType", typeof(int))});
dt.Rows.Add("Multiple Choices", 1);
dt.Rows.Add("Fill Blanks", 2);
gvQuestionBank.DataSource = dt;
gvQuestionBank.DataBind();
}
}
private static DataTable GetQuestions()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("QType", typeof(int)),
new DataColumn("Question", typeof(string)),
new DataColumn("OptionA", typeof(string)),
new DataColumn("OptionB", typeof(string)),
new DataColumn("OptionC", typeof(string)),
new DataColumn("OptionD", typeof(string)),
new DataColumn("Answer", typeof(string)) });
dt.Rows.Add(1, "Computer Word is taken from the word", "Compute", "Calculation", "Component", "Compare", "");
dt.Rows.Add(1, "A Computer is an", "Mechanical", "Electronic", "Chemical", "Biological", "");
dt.Rows.Add(1, "Data is a Collection of raw", "Facts and Figures", "Information", "a & b", "None", "");
dt.Rows.Add(2, "Fill Blanks 1", "a", "b", "c", "d", "b");
dt.Rows.Add(2, "Fill Blanks 2", "a", "b", "c", "d", "a");
dt.Rows.Add(2, "Fill Blanks 3", "a", "b", "c", "d", "c");
return dt;
}
protected void Generate(object sender, EventArgs e)
{
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
DataTable dt1 = GetQuestions();
for (int i = 0; i < gvQuestionBank.Rows.Count; i++)
{
string questionType = (gvQuestionBank.Rows[i].Cells[0].FindControl("hfValue") as HiddenField).Value;
int noOfQuestion = Convert.ToInt32((gvQuestionBank.Rows[i].Cells[1].FindControl("txtNo") as TextBox).Text);
string qCategory = (gvQuestionBank.Rows[i].FindControl("lblCategory") as Label).Text;
var dr = dt1.Select("QType=" + questionType).Take(noOfQuestion).ToList();
if (qCategory == "Multiple Choices")
{
document.Add(new Paragraph("Q" + (i + 1) + ": Choose suitable answer from Multiple Choice Question"));
}
else if (qCategory == "Fill Blanks")
{
document.Add(new Paragraph("Q" + (i + 1) + ": Fill Blanks given following"));
}
if (noOfQuestion > 0)
{
for (int j = 0; j < dr.Count; j++)
{
string question = dr[j]["Question"].ToString();
string OptionA = dr[j]["OptionA"].ToString();
string OptionB = dr[j]["OptionB"].ToString();
string OptionC = dr[j]["OptionC"].ToString();
string OptionD = dr[j]["OptionD"].ToString();
OptionA = !string.IsNullOrEmpty(OptionA) ? " A)" + OptionA : "";
OptionB = !string.IsNullOrEmpty(OptionB) ? " B)" + OptionB : "";
OptionC = !string.IsNullOrEmpty(OptionC) ? " C)" + OptionC : "";
OptionD = !string.IsNullOrEmpty(OptionD) ? " D)" + OptionD : "";
if (dr[0][0].ToString() == "1")
{
document.Add(new Paragraph((j + 1) + "." + question + "_______ ", new Font(Font.FontFamily.TIMES_ROMAN, 8)));
document.Add(new Paragraph(OptionA + OptionB + OptionC + OptionD, new Font(Font.FontFamily.TIMES_ROMAN, 8)));
}
else if (dr[0][0].ToString() == "2")
{
document.Add(new Paragraph((j + 1) + "." + question + "_______ ", new Font(Font.FontFamily.TIMES_ROMAN, 8)));
document.Add(new Paragraph(OptionA + OptionB + OptionC + OptionD, new Font(Font.FontFamily.TIMES_ROMAN, 8)));
}
}
document.Add(new Paragraph(" "));
}
}
document.Add(new Paragraph(" "));
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
Screenshot