【发布时间】:2014-10-01 05:24:05
【问题描述】:
我尝试使用以下代码将 Word 文档的所有页面保存为增强型元文件 (.emf) 图像:
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分别获取每个页面的内容?
【问题讨论】:
-
为什么有这么多
ref objectMissings? -
这段代码不是我的,它的例子。我不使用'ref objectMissing'
-
请不要只求我们为您解决问题。向我们展示您如何尝试自己解决问题,然后向我们确切地展示结果是什么,并告诉我们您为什么觉得它不起作用。请参阅“What Have You Tried?”了解您真正需要阅读的优秀文章。仅仅向我们展示您从某处复制的代码是不够的。为了实现自己的目标,您做了什么尝试?
-
哇,放松,我试图将 doc 保存到图像,但是 'Content.EnhMetaFileBits;'返回第一个文档页面的字节,但我需要将所有文档保存为图像
-
您的代码甚至没有显示
EnhMetaFileBits是什么,因为我们不知道wordApp的类型是什么,您使用的是什么库等等。