0
votes

I want to export MS word(docx/doc) document pages to Image(jpeg/png).

I am doing same for presentation(pptx/ppt) using office interop export api for each slide, but didn't found corresponding API for word.

Need suggestion for API/alternate approach for achieving this.

1

1 Answers

3
votes

Based on this similar question: "Saving a word document as an image" you could do something like this:

const string basePath = @"C:\Users\SomeUser\SomePath\";
var docPath = Path.Combine(basePath, "documentA.docx");
var app = new Application()
                {
                    Visible = true
                };

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

foreach (Window window in doc.Windows)
{
    foreach (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(basePath, string.Format("page-no-{0}", i));

            using (var ms = new MemoryStream(bits))
            {
                var image = Image.FromStream(ms);
                var pngTarget = Path.ChangeExtension(target, "png");
                image.Save(pngTarget, ImageFormat.Png);
            }
        }
    }
}

app.Quit();

Basically, I'm using the Page.EhmMetaFileBits property which, according to the documentation:

Returns a Object that represents a picture representation of how a page of text appears.

... and based on that, I create an image and save it to the disk.