Generate questions and options PDF from GridView using iTextSharp in ASP.Net

Last Reply 2 months ago By pandeyism

Posted 2 months ago

I have a question bank history like this in the grid view

Question

OptionA

OptionB

OptionC

OptionD

Answer

Multiple Choice Question 1

Choice1

Choice2

Choice3

Choice4

Choice1

Multiple Choice Question 2

Choice1

Choice2

Choice3

Choice4

Choice4

Multiple Choice Question 3

Choice1

Choice2

Choice3

Choice4

Choice2

Fill Blanks 1

a

b

 

 

b

Fill Blanks 2

a

b

 

 

a

Fill Blanks 3

a

b

 

 

a

Good

 

 

 

 

 

Kind

 

 

 

 

 

Act

 

 

 

 

 

Work

 

 

 

 

 

Habit

 

 

 

 

 

I get up early in the morning and goes for walk daily. It is a good for my health.

 

 

 

 

 

I have defined Question Type and Question Category such as 

QType

QTypeID QTypeName

1          Objctive

2          Subjective

QCategory

QCatID    QCatName              QTypeID

1            Multiple Choice          1

2            Fill Blanks                  1

3            Pharse Sentence         2

4            Translation                 2

Now I want to Preview and generate paper from this above grid view to in the following format in the pdf file. How to make it pls guide.

Q1: Choose suitable answer from Multiple Choice Question

  1. This is multiple choices1.                               A) Choice1 B) Choice2 C) Choice3 D) Choice4
  2. This is multiple choices2.                               A) Choice1 B) Choice2 C) Choice3 D) Choice4
  3. This is multiple choices3.                               A) Choice1 B) Choice2 C) Choice3 D) Choice4

Q2: Fill Blanks given following

  1. This is blanks 1.                                                 A) a        B) b
  2. This is blanks 2.                                                 A) a        B) b
  3. This is blanks 3.                                                 A) a        B) b

Q3: Phrase Sentences of the given words

                Good, Kind, Act, Work, Habit

Q4: Translate the sentence into urdu.

                I get up early in the morning and goes for walk daily. It have a good effect upon my health.

 

 

You are viewing reply posted by: pandeyism 2 months ago.
Posted 2 months ago

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