ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Generate questions and options PDF from GridView using iTextSharp in ASP.Net<p>Hi <a class="username" rel="smile"> smile</a>,</p> <p>Please refer below smaple.</p> <p>Refer below code and modifiy as per your requirement.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div&gt; &lt;asp:GridView ID=&#34;gvQuestionBank&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;QType&#34; HeaderText=&#34;QType&#34; /&gt; &lt;asp:BoundField DataField=&#34;Question&#34; HeaderText=&#34;Question&#34; /&gt; &lt;asp:BoundField DataField=&#34;OptionA&#34; HeaderText=&#34;OptionA&#34; /&gt; &lt;asp:BoundField DataField=&#34;OptionB&#34; HeaderText=&#34;OptionB&#34; /&gt; &lt;asp:BoundField DataField=&#34;OptionC&#34; HeaderText=&#34;OptionC&#34; /&gt; &lt;asp:BoundField DataField=&#34;OptionD&#34; HeaderText=&#34;OptionD&#34; /&gt; &lt;asp:BoundField DataField=&#34;Answer&#34; HeaderText=&#34;Answer&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:Button ID=&#34;btnGenerate&#34; OnClick=&#34;Generate&#34; Text=&#34;Generate&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; using System.Linq; using System.Collections.Generic;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;QType&#34;, typeof(int)), new DataColumn(&#34;Question&#34;, typeof(string)), new DataColumn(&#34;OptionA&#34;, typeof(string)), new DataColumn(&#34;OptionB&#34;, typeof(string)), new DataColumn(&#34;OptionC&#34;, typeof(string)), new DataColumn(&#34;OptionD&#34;, typeof(string)), new DataColumn(&#34;Answer&#34;, typeof(string)) }); dt.Rows.Add(1, &#34;Multiple Choice Question 1&#34;, &#34;Choice1&#34;, &#34;Choice2&#34;, &#34;Choice3&#34;, &#34;Choice4&#34;, &#34;Choice1&#34;); dt.Rows.Add(1, &#34;Multiple Choice Question 2&#34;, &#34;Choice1&#34;, &#34;Choice2&#34;, &#34;Choice3&#34;, &#34;Choice4&#34;, &#34;Choice4&#34;); dt.Rows.Add(1, &#34;Multiple Choice Question 3&#34;, &#34;Choice1&#34;, &#34;Choice2&#34;, &#34;Choice3&#34;, &#34;Choice4&#34;, &#34;Choice2&#34;); dt.Rows.Add(2, &#34;Fill Blanks 1&#34;, &#34;a&#34;, &#34;b&#34;, &#34;&#34;, &#34;&#34;, &#34;b&#34;); dt.Rows.Add(2, &#34;Fill Blanks 2&#34;, &#34;a&#34;, &#34;b&#34;, &#34;&#34;, &#34;&#34;, &#34;a&#34;); dt.Rows.Add(2, &#34;Fill Blanks 3&#34;, &#34;a&#34;, &#34;b&#34;, &#34;&#34;, &#34;&#34;, &#34;a&#34;); dt.Rows.Add(3, &#34;Good&#34;); dt.Rows.Add(3, &#34;Kind&#34;); dt.Rows.Add(3, &#34;Act&#34;); dt.Rows.Add(3, &#34;Work&#34;); dt.Rows.Add(3, &#34;Habit&#34;); dt.Rows.Add(4, &#34;I get up early in the morning and goes for walk daily. It have a good effect upon my health.&#34;); dt.DefaultView.Sort = &#34;QType&#34;; gvQuestionBank.DataSource = dt; gvQuestionBank.DataBind(); ViewState[&#34;dt&#34;] = 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&lt;DataTable&gt; dtGroup = (ViewState[&#34;dt&#34;] as DataTable).AsEnumerable().GroupBy(row =&gt; row.Field&lt;int&gt;(&#34;QType&#34;)).Select(g =&gt; g.CopyToDataTable()).ToList(); for (int i = 0; i &lt; dtGroup.Count; i++) { DataTable dt = dtGroup[i]; if (dt.Rows[0][0].ToString() == &#34;1&#34;) { document.Add(new Paragraph(&#34;Q&#34; + (i + 1) + &#34;: Choose suitable answer from Multiple Choice Question&#34;)); } else if (dt.Rows[0][0].ToString() == &#34;2&#34;) { document.Add(new Paragraph(&#34;Q&#34; + (i + 1) + &#34;: Fill Blanks given following&#34;)); } else if (dt.Rows[0][0].ToString() == &#34;3&#34;) { document.Add(new Paragraph(&#34;Q&#34; + (i + 1) + &#34;: Phrase Sentences of the given words&#34;)); } else if (dt.Rows[0][0].ToString() == &#34;4&#34;) { document.Add(new Paragraph(&#34;Q&#34; + (i + 1) + &#34;: Translate the sentence into urdu&#34;)); } string pharseSentence = &#34;&#34;; int count = 1; for (int j = 0; j &lt; dt.Rows.Count; j++) { string question = dt.Rows[0][&#34;Question&#34;].ToString(); string OptionA = dt.Rows[0][&#34;OptionA&#34;].ToString(); string OptionB = dt.Rows[0][&#34;OptionB&#34;].ToString(); string OptionC = dt.Rows[0][&#34;OptionC&#34;].ToString(); string OptionD = dt.Rows[0][&#34;OptionD&#34;].ToString(); OptionA = !string.IsNullOrEmpty(OptionA) ? &#34; A)&#34; + OptionA : &#34;&#34;; OptionB = !string.IsNullOrEmpty(OptionB) ? &#34; B)&#34; + OptionB : &#34;&#34;; OptionC = !string.IsNullOrEmpty(OptionC) ? &#34; C)&#34; + OptionC : &#34;&#34;; OptionD = !string.IsNullOrEmpty(OptionD) ? &#34; D)&#34; + OptionD : &#34;&#34;; if (dt.Rows[0][0].ToString() == &#34;1&#34;) { document.Add(new Paragraph((j + 1) + &#34;.&#34; + question + &#34; &#34; + OptionA + OptionB + OptionC + OptionD, new Font(Font.FontFamily.TIMES_ROMAN, 8))); } else if (dt.Rows[0][0].ToString() == &#34;2&#34;) { document.Add(new Paragraph((j + 1) + &#34;.&#34; + question + &#34; &#34; + OptionA + OptionB + OptionC + OptionD, new Font(Font.FontFamily.TIMES_ROMAN, 8))); } else if (dt.Rows[0][0].ToString() == &#34;3&#34; || dt.Rows[0][0].ToString() == &#34;4&#34;) { pharseSentence += dt.Rows[j][&#34;Question&#34;].ToString() + &#34;,&#34;; if (dt.Rows.Count == count) { document.Add(new Paragraph(pharseSentence.TrimEnd(&#39;,&#39;), new Font(Font.FontFamily.TIMES_ROMAN, 8))); } count++; } } document.Add(new Paragraph(&#34; &#34;)); } document.Close(); byte[] bytes = memoryStream.ToArray(); memoryStream.Close(); Response.Clear(); Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;Content-Disposition&#34;, &#34;attachment; filename=Employee.pdf&#34;); Response.ContentType = &#34;application/pdf&#34;; 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 */ }</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/49DmDF0.jpg" alt="" width="472" height="354" /></p>https://www.aspforums.net:443/Threads/152861/Generate-questions-and-options-PDF-from-GridView-using-iTextSharp-in-ASPNet/https://www.aspforums.net:443/Threads/152861/Generate-questions-and-options-PDF-from-GridView-using-iTextSharp-in-ASPNet/Wed, 10 Oct 2018 07:54:54 GMT