Hi Waghmare,
Refer below modified code.
C#
protected void btnGenerete_Click(object sender, EventArgs e)
{
object fileName = Server.MapPath("~/Files/Test.docx");
if (System.IO.File.Exists(fileName.ToString()))
{
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.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);
doc.Activate();
foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
{
foreach (Microsoft.Office.Interop.Word.Pane pane in window.Panes)
{
for (var i = 1; i <= pane.Pages.Count; i++)
{
var bits = pane.Pages[i].EnhMetaFileBits;
var target = System.IO.Path.Combine("\\" + fileName.ToString().Split('.')[0], string.Format("{1}_page_{0}", i, fileName.ToString().Split('.')[0]));
try
{
using (var ms = new System.IO.MemoryStream((byte[])(bits)))
{
var image = System.Drawing.Image.FromStream(ms);
var pngTarget = System.IO.Path.ChangeExtension(target, "png");
image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
Response.Write("Image created with Name" + target);
}
}
catch (System.Exception ex)
{ }
}
}
}
doc.Close(Type.Missing, Type.Missing, Type.Missing);
word.Quit(Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
doc.Close(ref missing, ref missing, ref missing);
((Microsoft.Office.Interop.Word._Application)word).Quit();
}
}
else
{
Response.Write("File not Found !!!");
}
}
VB.Net
Protected Sub btnGenerete_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim fileName As Object = Server.MapPath("~/Files/Test.docx")
If IO.File.Exists(fileName.ToString()) Then
Dim word As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
Dim doc As Microsoft.Office.Interop.Word.Document = New Microsoft.Office.Interop.Word.Document()
Dim missing As Object = System.Type.Missing
Try
doc = word.Documents.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
doc.Activate()
For Each window As Microsoft.Office.Interop.Word.Window In doc.Windows
For Each pane As Microsoft.Office.Interop.Word.Pane In window.Panes
For i = 1 To pane.Pages.Count
Dim bits = pane.Pages(i).EnhMetaFileBits
Dim target = IO.Path.Combine("\" & fileName.ToString().Split("."c)(0), String.Format("{1}_page_{0}", i, fileName.ToString().Split("."c)(0)))
Try
Using ms = New IO.MemoryStream(CType((bits), Byte()))
Dim image = System.Drawing.Image.FromStream(ms)
Dim pngTarget = IO.Path.ChangeExtension(target, "png")
image.Save(pngTarget, Drawing.Imaging.ImageFormat.Png)
Response.Write("Image created with Name" & target)
End Using
Catch ex As System.Exception
End Try
Next
Next
Next
doc.Close(Type.Missing, Type.Missing, Type.Missing)
word.Quit(Type.Missing, Type.Missing, Type.Missing)
Catch ex As Exception
Response.Write(ex.Message)
Finally
doc.Close(missing, missing, missing)
CType(word, Microsoft.Office.Interop.Word._Application).Quit()
End Try
Else
Response.Write("File not Found !!!")
End If
End Sub