如何将Word文档的所有页面保存为图像
本文关键字:保存 图像 Word 文档 | 更新日期: 2023-09-27 18:19:21
我尝试使用以下代码将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
?
我认为你需要改变:
byte[] bytes = (byte[])app.ActiveDocument.Content.EnhMetaFileBits;
:
byte[] bytes = (byte[])app.ActiveDocument.Range().EnhMetaFileBits;