Hi smile,
Please refer below smaple.
Refer below code and modifiy as per your requirement.
HTML
<div>
<asp:GridView ID="gvQuestionBank" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="QType" HeaderText="QType" />
<asp:BoundField DataField="Question" HeaderText="Question" />
<asp:BoundField DataField="OptionA" HeaderText="OptionA" />
<asp:BoundField DataField="OptionB" HeaderText="OptionB" />
<asp:BoundField DataField="OptionC" HeaderText="OptionC" />
<asp:BoundField DataField="OptionD" HeaderText="OptionD" />
<asp:BoundField DataField="Answer" HeaderText="Answer" />
</Columns>
</asp:GridView>
<asp:Button ID="btnGenerate" OnClick="Generate" Text="Generate" runat="server" />
</div>
Namespaces
C#
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Linq;
using System.Collections.Generic;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
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, "Multiple Choice Question 1", "Choice1", "Choice2", "Choice3", "Choice4", "Choice1");
dt.Rows.Add(1, "Multiple Choice Question 2", "Choice1", "Choice2", "Choice3", "Choice4", "Choice4");
dt.Rows.Add(1, "Multiple Choice Question 3", "Choice1", "Choice2", "Choice3", "Choice4", "Choice2");
dt.Rows.Add(2, "Fill Blanks 1", "a", "b", "", "", "b");
dt.Rows.Add(2, "Fill Blanks 2", "a", "b", "", "", "a");
dt.Rows.Add(2, "Fill Blanks 3", "a", "b", "", "", "a");
dt.Rows.Add(3, "Good");
dt.Rows.Add(3, "Kind");
dt.Rows.Add(3, "Act");
dt.Rows.Add(3, "Work");
dt.Rows.Add(3, "Habit");
dt.Rows.Add(4, "I get up early in the morning and goes for walk daily. It have a good effect upon my health.");
dt.DefaultView.Sort = "QType";
gvQuestionBank.DataSource = dt;
gvQuestionBank.DataBind();
ViewState["dt"] = 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();
List<DataTable> dtGroup = (ViewState["dt"] as DataTable).AsEnumerable().GroupBy(row => row.Field<int>("QType")).Select(g => g.CopyToDataTable()).ToList();
for (int i = 0; i < dtGroup.Count; i++)
{
DataTable dt = dtGroup[i];
if (dt.Rows[0][0].ToString() == "1")
{
document.Add(new Paragraph("Q" + (i + 1) + ": Choose suitable answer from Multiple Choice Question"));
}
else if (dt.Rows[0][0].ToString() == "2")
{
document.Add(new Paragraph("Q" + (i + 1) + ": Fill Blanks given following"));
}
else if (dt.Rows[0][0].ToString() == "3")
{
document.Add(new Paragraph("Q" + (i + 1) + ": Phrase Sentences of the given words"));
}
else if (dt.Rows[0][0].ToString() == "4")
{
document.Add(new Paragraph("Q" + (i + 1) + ": Translate the sentence into urdu"));
}
string pharseSentence = "";
int count = 1;
for (int j = 0; j < dt.Rows.Count; j++)
{
string question = dt.Rows[0]["Question"].ToString();
string OptionA = dt.Rows[0]["OptionA"].ToString();
string OptionB = dt.Rows[0]["OptionB"].ToString();
string OptionC = dt.Rows[0]["OptionC"].ToString();
string OptionD = dt.Rows[0]["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 (dt.Rows[0][0].ToString() == "1")
{
document.Add(new Paragraph((j + 1) + "." + question + " " + OptionA + OptionB + OptionC + OptionD, new Font(Font.FontFamily.TIMES_ROMAN, 8)));
}
else if (dt.Rows[0][0].ToString() == "2")
{
document.Add(new Paragraph((j + 1) + "." + question + " " + OptionA + OptionB + OptionC + OptionD, new Font(Font.FontFamily.TIMES_ROMAN, 8)));
}
else if (dt.Rows[0][0].ToString() == "3" || dt.Rows[0][0].ToString() == "4")
{
pharseSentence += dt.Rows[j]["Question"].ToString() + ",";
if (dt.Rows.Count == count)
{
document.Add(new Paragraph(pharseSentence.TrimEnd(','), new Font(Font.FontFamily.TIMES_ROMAN, 8)));
}
count++;
}
}
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();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Screenshot