5

How can I convert word document pages to particular images?

I have used following code to convert it to .tiff image

object varMissing = Type.Missing;

object varFalseValue = false;
object varTrueValue = true;

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
word.ActivePrinter = "Microsoft XPS Document Writer";
object fileName = startupPath + "\\" + filename1;
object falseValue = false;
object trueValue = true;
object missing = Type.Missing;
word.Visible = true;
word.Activate();
Document doc = word.Documents.Open(ref fileName, ref missing,
                    ref falseValue, 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();

object PrintToFile = true;
object OutputFileName = startupPath + "\\" + filename1.Split('.')[0] + ".tif";

doc.PrintOut(ref varMissing, ref varFalseValue, ref varMissing, 
             ref OutputFileName, ref varMissing, ref varMissing, ref varMissing,
             ref varMissing, ref varMissing, ref varMissing, ref PrintToFile, 
             ref varMissing, ref varMissing, ref varMissing, ref varMissing, 
             ref varMissing, ref varMissing, ref varMissing);

doc.Close(ref varMissing, ref varMissing, ref varMissing);
word.Quit(ref varMissing, ref varMissing, ref varMissing);

//Multi to Single
MemoryStream ms;
Image myImage;

FileStream fs = new FileStream(startupPath + "\\" + filename1.Split('.')[0] + ".tif", FileMode.Open);
fs.Seek(0, SeekOrigin.Begin);

//Here I am getting error....
myImage = Image.FromStream(fs, true, false);

Guid myGuid = myImage.FrameDimensionsList[0];
FrameDimension myDimension = new FrameDimension(myGuid);
int myPageCount = myImage.GetFrameCount(myDimension);

for (int i = 0; i < myPageCount; i++)
{
      ms = new MemoryStream();
      myImage.SelectActiveFrame(myDimension, i);
      myImage.Save(string.Format(startupPath + "\\" + filename1.Split('.')[0] + ".tif", i), ImageFormat.Tiff);
}

fs.Close();

But when I am trying to convert file stream to Image it gives me error like

Parameter is not valid.

Is there any other way to convert word file to image file?? I cannot use the dlls which are available in market for sell but I need to use the Microsoft's core interop dll. So please can anyone suggest me the link or way to convert .doc file to .jpg.

Rahul Gokani
  • 1,688
  • 5
  • 25
  • 43
  • What line do you get your error on? – JMK Dec 02 '13 at 11:15
  • When I am converting FileStram object fs to Image object myImage. – Rahul Gokani Dec 02 '13 at 11:20
  • Good joke. [`Image.FromStream`](http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx) is for converting a stream _which contains an image_ into an image in memory. It doesn't convert any arbitrary stream into an image! – John Saunders Dec 02 '13 at 11:29
  • Thank you for complement sir. I have find that code from other web site. And most of the post I have seen with the same code which I have applied. my reference link is https://groups.google.com/forum/#!topic/microsoft.public.word.conversions/HELHX-iXMsE. So can you tell me that how can I convert doc file to image file. – Rahul Gokani Dec 02 '13 at 11:34
  • And the other thing sir, That Before converting the filestream to image in my code I have put some code which creates .tif file. So in that stream I am getting the .tif file stream. not any arbitrary stream. – Rahul Gokani Dec 02 '13 at 11:36
  • @JohnSaunders so can you give me any reference that I can use to convert doc file to image file. – Rahul Gokani Dec 02 '13 at 11:37
  • My bad. I was unable or unwilling to follow the logic of your code to see that you first produced a .tif file. Suggestion: is it even _relevant_ that the .tif file you're loading is from printing a word document to a file? Does your "convert to Image" code work with any other kind of .tif file, or is the problem only with .tif files from "print to file"? Does it work if you print some other kind of document to a .tif file? – John Saunders Dec 02 '13 at 11:41
  • The logic gives me error for all the documents. And I haven't try to convert the other created or working tiff file using my logic. But in some link I have find that the logic gives this kind of error when the stream is not in the correct format. So whenever I am converting this tif file it seems not to be in correct format. – Rahul Gokani Dec 02 '13 at 11:45
  • So is there any other way? – Rahul Gokani Dec 02 '13 at 11:45

2 Answers2

13

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();
  • When i converting doc to images while i getting following error : The requested member of the collection does not exist. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.InteropServices.COMException: The requested member of the collection does not exist. – Sagar Rawal Apr 18 '14 at 07:29
  • How to merge all images to one file? – Kumar Gaurav Sep 02 '14 at 11:15
  • Not very useful, it usually throw error as mentioned above. it's better use Printer or third party component – MRP Sep 14 '14 at 04:11
  • The resulted image is too large and minimizing doesn't produce any good quality, please advise. – Wessam El Mahdy Jan 02 '17 at 17:10
  • I get an error on calling the methods Register & Revoke. On which DLL file they can be found? Thanks – shahar eldad Feb 24 '19 at 13:51
3

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);
honk
  • 9,137
  • 11
  • 75
  • 83
elnashillo
  • 61
  • 9