Click here to Skip to main content
14,207,870 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi, how to convert word file to image file using c#?
Posted
Comments
ZurdoDev 28-Jan-15 7:43am
   
A Microsoft Word file? To an image? Why would you want to do that?

And, where are you stuck?
sankarisiva 28-Jan-15 7:49am
   
return File(fileDetails.FilePath, "application/msword"); i'm using this code to view word document it did't open any word file it produces a popup that says you want to open or download the file,my requirement is my client only able to see the file not to download the file so only i'm trying to convert word to image files.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello,

You can use below code to convert word to jpeg.it works 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();
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190612.1 | Last Updated 12 Feb 2015
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100