gpt4 book ai didi

c# - 如何将Word文档的所有页面保存为图片?

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:50 24 4
gpt4 key购买 nike

我尝试使用以下代码将 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;

关于c# - 如何将Word文档的所有页面保存为图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25477994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com