Convert Word file pages to jpg images using C#

Solution 1:

You can convert Doc file to image using bellow code it worked for me.

var docPath = Path.Combine(startupPath, filename1);
var app = new Microsoft.Office.Interop.Word.Application();

MessageFilter.Register();

app.Visible = true;

var doc = app.Documents.Open(docPath);

doc.ShowGrammaticalErrors = false;
doc.ShowRevisions = false;
doc.ShowSpellingErrors = false;

if (!Directory.Exists(startupPath + "\\" + filename1.Split('.')[0]))
{
     Directory.CreateDirectory(startupPath + "\\" + filename1.Split('.')[0]);
}

//Opens the word document and fetch each page and converts to image
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 page = pane.Pages[i];
                 var bits = page.EnhMetaFileBits;
                 var target = Path.Combine(startupPath + "\\" + filename1.Split('.')[0], string.Format("{1}_page_{0}", i, filename1.Split('.')[0]));

                 try
                 {
                     using (var ms = new MemoryStream((byte[])(bits)))
                     {
                          var image = System.Drawing.Image.FromStream(ms);
                          var pngTarget = Path.ChangeExtension(target, "png");
                          image.Save(pngTarget, ImageFormat.Png);
                     }
                 }
                 catch (System.Exception ex)
                 { }
         }
    }
}
doc.Close(Type.Missing, Type.Missing, Type.Missing);
app.Quit(Type.Missing, Type.Missing, Type.Missing);
MessageFilter.Revoke();

Solution 2:

I think I've found the error on the code provided by @WarLock. I changed some parts of it and did it with only one file:

Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
Document myWordDoc = new Document();
object missing = System.Type.Missing;
object path1= path + filename + ".doc";
myWordDoc = myWordApp.Documents.Add(path1, missing, missing, missing);

foreach (Microsoft.Office.Interop.Word.Window window in myWordDoc.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 =path1 + "_image.doc";
            try
            {
                using (var ms = new MemoryStream((byte[])(bits)))
                {
                    var image = System.Drawing.Image.FromStream(ms);
                    var pngTarget = Path.ChangeExtension(target, "png");
                    image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
                }
            }
            catch (System.Exception ex)
            { }
        }
    }
}
myWordDoc.Close(Type.Missing, Type.Missing, Type.Missing);
myWordApp.Quit(Type.Missing, Type.Missing, Type.Missing);