首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Word文档的所有页面保存为图像?

如何将Word文档的所有页面保存为图像?
EN

Stack Overflow用户
提问于 2014-08-25 09:41:32
回答 1查看 2.1K关注 0票数 2

我尝试使用以下代码将Word文档的所有页面另存为增强型图元文件(.emf)图像:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using word = Microsoft.Office.Interop.Word;
using System.Drawing.Imaging;
using System.Drawing;

namespace WordToImg
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Doc|*.doc;*.docx";
            ofd.Title = "Select file to convert";
            ofd.InitialDirectory=Application.StartupPath;
            if (ofd.ShowDialog()==DialogResult.OK)
            {
                string file = ofd.FileName;
                word.Application app = new word.Application();
                word.Document doc = app.Documents.Open(file);

                byte[] bytes = (byte[])app.ActiveDocument.Content.EnhMetaFileBits;
                if (bytes != null)
                {
                    MemoryStream ms = new MemoryStream(bytes);
                    Image temp = Image.FromStream(ms);
                    temp.Save(Path.GetDirectoryName(file) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(file) + ".png", ImageFormat.Png);
                }
                doc.Close(false);
                doc = null;
                app.Quit();
                GC.Collect();
            }
        }
    }
}

但是,创建的图元文件只包含第一页的内容。有没有办法以图像的形式获取整个文档内容?或者,可以使用Content.EnhMetaFileBits分别获取每个页面的内容

EN

回答 1

Stack Overflow用户

发布于 2014-10-01 05:24:06

我认为你需要改变:

代码语言:javascript
复制
 byte[] bytes = (byte[])app.ActiveDocument.Content.EnhMetaFileBits;

至:

代码语言:javascript
复制
 byte[] bytes = (byte[])app.ActiveDocument.Range().EnhMetaFileBits;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25477994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档