Read MS Word file and Convert each question to PNG using C# in ASP.Net

Last Reply on Sep 16, 2016 08:51 AM By Indresh

Posted on Sep 16, 2016 01:48 AM

Hi! I have file Ms Word with Question and answer. But I want convert each question and answer to png. After convert create folder and save png into folder. For example I have question and answer:

Input:

@1.The capital of India.

$A) Dushanbe;

$B) Moscow;  

©C) Delhi;

$D) Kabul.

@2.The capital of Tajikistan.

©A) Dushanbe; $B) Moscow;  $C) Delhi; $D) Kabul.

Output png file:

1.The capital of India.

A) Dushanbe;

B) Moscow;  

C) Delhi;

D) Kabul.

2.The capital of Tajikistan.

A) Dushanbe; B) Moscow;  C) Delhi; D) Kabul.

 

Posted on Sep 16, 2016 08:51 AM

Hi RPA,

I have created sample code which fullfill requirement.

HTML

<div>
    <asp:Button ID="btnImport" runat="server" Text="Import" OnClick="Import" />
    <br />
    <br />
    <asp:GridView ID="gvQuestions" runat="server" AutoGenerateColumns="true" />
    <br />
    <asp:Button ID="btnPng" Text="ConvertPng" runat="server" OnClick="Convertpng" />
    <asp:Label ID="lblConvert" Text="" runat="server" />
</div>

C#

protected void Import(object sender, EventArgs e)
{

    Application word = new Application();
    Document doc = new Document();

    string filePath = Server.MapPath("~/Files/Test.docx");
    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<string> data = new List<string>();
    for (int i = 0; i < 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("Id",typeof(int)),
new System.Data.DataColumn("Question"),
new System.Data.DataColumn("Answer1"),
new System.Data.DataColumn("Answer2"),
new System.Data.DataColumn("Answer3"),
new System.Data.DataColumn("Answer4"),
new System.Data.DataColumn("CorrectAnswer")                                  
});
    int totalQuestionCount = 0;
    int rowNo = 1;
    System.Data.DataRow dr = dt.NewRow();
    for (int i = 0; i < data.Count; i++)
    {
        string type = string.Empty;
        if (data[i].Contains("$") || data[i].Contains("©"))
        {
            type = "option";
            if (i != 0)
            {
                if ((!data[i - 1].Contains("@")))
                {
                    type = "new line option";
                }
            }
        }
        else
        {
            type = "question";
        }
        int optionCount = 2;
        switch (type)
        {
            case "question":
                dr[0] = rowNo;
                dr[1] = data[i].Replace("@", "").Trim();
                rowNo++;
                break;
            case "option":

                foreach (string option in data[i].Split(';'))
                {
                    if (!string.IsNullOrEmpty(option))
                    {
                        if (option.Contains("$"))
                        {
                            dr[optionCount] = option.Replace("$", "").Trim();
                        }
                        else if (option.Contains("©"))
                        {
                            //* Option*//

                            dr[optionCount] = option.Replace("©", "").Trim();
                            dr[6] = option.Split(')')[1].Trim();
                        }
                        optionCount++;
                    }
                }
                break;
            case "new line option":
                foreach (string option in data[i].Split(';'))
                {
                    if (!string.IsNullOrEmpty(option))
                    {

                        int colIndex = 0;

                        if (option.Contains("$A") || option.Contains("©A"))
                        {
                            colIndex = 2;
                        }
                        if (option.Contains("$B") || option.Contains("©B"))
                        {
                            colIndex = 3;
                        }
                        if (option.Contains("$C") || option.Contains("©C"))
                        {
                            colIndex = 4;
                        }
                        if (option.Contains("$D") || option.Contains("©D"))
                        {
                            colIndex = 5;
                        }
                        dt.Rows[dt.Rows.Count - 1][colIndex] = option.Replace("$", "").Trim();
                        if (option.Contains("©"))
                        {
                            dt.Rows[dt.Rows.Count - 1][colIndex] = option.Replace("©", "").Trim();
                            dt.Rows[dt.Rows.Count - 1][6] = option.Split(')')[1].Trim();
                        }
                    }
                }
                break;
        }
        if (i != 0 && data[i - 1].Contains("@"))
        {
            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("Arial", 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()) + ".png";
        bitmap.Save(Server.MapPath("~/Files/") + fileName, ImageFormat.Png);
        lblConvert.Text = "Done";
    }
}

Screenshot

I hope works for you.