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.Read MS Word file and Convert each question to PNG using C# in ASP.Net<p>Hi <a class="username" rel="RPA">RPA</a>,</p> <p>I will get back to you.</p>https://www.aspforums.net:443/Threads/661236/Read-MS-Word-file-and-Convert-each-question-to-PNG-using-C-in-ASPNet/https://www.aspforums.net:443/Threads/661236/Read-MS-Word-file-and-Convert-each-question-to-PNG-using-C-in-ASPNet/Fri, 16 Sep 2016 01:52:53 GMTRead MS Word file and Convert each question to PNG using C# in ASP.Net<p>Hi <a class="username" rel="RPA"> RPA</a>,</p> <p>I have created sample code which fullfill requirement.</p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html">&lt;div&gt; &lt;asp:Button ID=&#34;btnImport&#34; runat=&#34;server&#34; Text=&#34;Import&#34; OnClick=&#34;Import&#34; /&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvQuestions&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;true&#34; /&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnPng&#34; Text=&#34;ConvertPng&#34; runat=&#34;server&#34; OnClick=&#34;Convertpng&#34; /&gt; &lt;asp:Label ID=&#34;lblConvert&#34; Text=&#34;&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">protected void Import(object sender, EventArgs e) { Application word = new Application(); Document doc = new Document(); string filePath = Server.MapPath(&#34;~/Files/Test.docx&#34;); object missing = System.Type.Missing; object fileName = filePath; doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); String read = string.Empty; List&lt;string&gt; data = new List&lt;string&gt;(); for (int i = 0; i &lt; doc.Paragraphs.Count; i++) { string temp = doc.Paragraphs[i + 1].Range.Text.Trim(); if (temp != string.Empty) data.Add(temp); } doc.Close(ref missing, ref missing, ref missing); System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.AddRange(new System.Data.DataColumn[] { new System.Data.DataColumn(&#34;Id&#34;,typeof(int)), new System.Data.DataColumn(&#34;Question&#34;), new System.Data.DataColumn(&#34;Answer1&#34;), new System.Data.DataColumn(&#34;Answer2&#34;), new System.Data.DataColumn(&#34;Answer3&#34;), new System.Data.DataColumn(&#34;Answer4&#34;), new System.Data.DataColumn(&#34;CorrectAnswer&#34;) }); int totalQuestionCount = 0; int rowNo = 1; System.Data.DataRow dr = dt.NewRow(); for (int i = 0; i &lt; data.Count; i++) { string type = string.Empty; if (data[i].Contains(&#34;$&#34;) || data[i].Contains(&#34;&copy;&#34;)) { type = &#34;option&#34;; if (i != 0) { if ((!data[i - 1].Contains(&#34;@&#34;))) { type = &#34;new line option&#34;; } } } else { type = &#34;question&#34;; } int optionCount = 2; switch (type) { case &#34;question&#34;: dr[0] = rowNo; dr[1] = data[i].Replace(&#34;@&#34;, &#34;&#34;).Trim(); rowNo++; break; case &#34;option&#34;: foreach (string option in data[i].Split(&#39;;&#39;)) { if (!string.IsNullOrEmpty(option)) { if (option.Contains(&#34;$&#34;)) { dr[optionCount] = option.Replace(&#34;$&#34;, &#34;&#34;).Trim(); } else if (option.Contains(&#34;&copy;&#34;)) { //* Option*// dr[optionCount] = option.Replace(&#34;&copy;&#34;, &#34;&#34;).Trim(); dr[6] = option.Split(&#39;)&#39;)[1].Trim(); } optionCount++; } } break; case &#34;new line option&#34;: foreach (string option in data[i].Split(&#39;;&#39;)) { if (!string.IsNullOrEmpty(option)) { int colIndex = 0; if (option.Contains(&#34;$A&#34;) || option.Contains(&#34;&copy;A&#34;)) { colIndex = 2; } if (option.Contains(&#34;$B&#34;) || option.Contains(&#34;&copy;B&#34;)) { colIndex = 3; } if (option.Contains(&#34;$C&#34;) || option.Contains(&#34;&copy;C&#34;)) { colIndex = 4; } if (option.Contains(&#34;$D&#34;) || option.Contains(&#34;&copy;D&#34;)) { colIndex = 5; } dt.Rows[dt.Rows.Count - 1][colIndex] = option.Replace(&#34;$&#34;, &#34;&#34;).Trim(); if (option.Contains(&#34;&copy;&#34;)) { dt.Rows[dt.Rows.Count - 1][colIndex] = option.Replace(&#34;&copy;&#34;, &#34;&#34;).Trim(); dt.Rows[dt.Rows.Count - 1][6] = option.Split(&#39;)&#39;)[1].Trim(); } } } break; } if (i != 0 &amp;&amp; data[i - 1].Contains(&#34;@&#34;)) { dt.Rows.Add(dr); totalQuestionCount++; dr = dt.NewRow(); } } ((_Application)word).Quit(); gvQuestions.DataSource = dt; gvQuestions.DataBind(); } protected void Convertpng(object sender, EventArgs e) { foreach (GridViewRow row in gvQuestions.Rows) { string question = row.Cells[1].Text; string Ans = row.Cells[2].Text + Environment.NewLine + row.Cells[3].Text + Environment.NewLine + row.Cells[4].Text + Environment.NewLine + row.Cells[5].Text; string fullQA = question + Environment.NewLine + Ans; Bitmap bitmap = new Bitmap(1, 1); System.Drawing.Font font = new System.Drawing.Font(&#34;Arial&#34;, 25, FontStyle.Regular, GraphicsUnit.Pixel); Graphics graphics = Graphics.FromImage(bitmap); int width = (int)graphics.MeasureString(fullQA, font).Width; int height = (int)graphics.MeasureString(fullQA, font).Height; bitmap = new Bitmap(bitmap, new Size(width, height)); graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.White); graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.TextRenderingHint = TextRenderingHint.AntiAlias; graphics.DrawString(fullQA, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0); graphics.Flush(); graphics.Dispose(); string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + &#34;.png&#34;; bitmap.Save(Server.MapPath(&#34;~/Files/&#34;) + fileName, ImageFormat.Png); lblConvert.Text = &#34;Done&#34;; } }</pre> <p><span style="text-decoration: underline;">Screenshot</span></p> <p><span style="text-decoration: underline;"><img src="https://i.imgur.com/gDAUiT6.gif" alt="" width="468" height="287" /></span></p> <p>I hope works for you.</p>https://www.aspforums.net:443/Threads/661236/Read-MS-Word-file-and-Convert-each-question-to-PNG-using-C-in-ASPNet/https://www.aspforums.net:443/Threads/661236/Read-MS-Word-file-and-Convert-each-question-to-PNG-using-C-in-ASPNet/Fri, 16 Sep 2016 08:51:01 GMT