Hi RPA,
I have created the sample. Refer the below code.
Code
protected void Page_Load(object sender, EventArgs e)
{
object fileName = Server.MapPath("~/Test.doc");
Application word = new Application();
Document doc = new Document();
object missing = System.Type.Missing;
try
{
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);
int questionNo = 1;
for (int paragraph = 0; paragraph < doc.Paragraphs.Count; paragraph++)
{
string temp = doc.Paragraphs[paragraph + 1].Range.Text.Trim();
// For Question
if (paragraph % 2 == 0)
{
int startIndex = doc.Paragraphs[paragraph + 1].Range.Text.Trim().IndexOf('.') + 1;
int totalLength = doc.Paragraphs[paragraph + 1].Range.Text.Trim().Length - 1;
string question = temp.Substring(startIndex, (totalLength - startIndex));
ConvertStringToImage(question, ("Qs" + questionNo));
questionNo++;
}
// For Options
else
{
int OptionNo = 1;
string[] options = temp.Split(';');
for (int option = 0; option < options.Length; option++)
{
if (!string.IsNullOrEmpty(options[option]))
{
int startIndex = options[option].IndexOf(')') + 1;
int totalLength = options[option].Length;
string singleOption = options[option].Substring(startIndex, (totalLength - startIndex)).Trim();
ConvertStringToImage(singleOption, ("Q" + (questionNo - 1) + "a" + OptionNo));
OptionNo++;
}
}
}
}
}
catch (Exception ex)
{
}
finally
{
doc.Close(ref missing, ref missing, ref missing);
((_Application)word).Quit();
}
}
private void ConvertStringToImage(string text, string name)
{
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(text, font).Width;
int height = (int)graphics.MeasureString(text, 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(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0);
graphics.Flush();
graphics.Dispose();
bitmap.Save(Server.MapPath("~/Images/") + name + ".png", ImageFormat.Png);
}
Input
@1.The capital of India. |
$A) Mumbai; $B) Chennai; ©C) Delhi; $D) Bangalore; |
@2.The capital of Tajikistan. |
©A) Dushanbe; $B) Khujand; $C) Kulob; $D) Qurghonteppa; |
@3.The capital of Bangladesh. |
$A) Barisal; $B) Chittagong; ©C) Khulna; $D) Dhaka; |
@4.The capital of SriLanka. |
©A) Colombo; $B) Negombo; $C) Kandy; $D) Kalmunai; |
Screenshot