用C#写一个word转图片的程序
以下是一个简单的C#程序,可以将Word文档转换为图片并保存到指定的文件夹中。该程序需要使用Microsoft.Office.Interop.Word库和System.Drawing.Imaging库。
using System;
using System.IO;
using System.Drawing.Imaging;
using Microsoft.Office.Interop.Word;
namespace WordToImageConverter
{
class Program
{
static void Main(string[] args)
{
// 设置Word文档路径
string wordFilePath = @"C:\test.docx";
// 设置输出图片路径
string outputFolder = @"C:\OutputImages\";
// 创建Word应用程序对象
Application app = new Application();
// 打开Word文档
Document doc = app.Documents.Open(wordFilePath);
// 遍历Word文档中的每一页
for (int i = 1; i <= doc.ComputeStatistics(WdStatistic.wdStatisticPages); i++)
{
// 选择当前页
doc.ActiveWindow.Selection.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, i);
// 将当前页转换为图片
string outputFileName = Path.Combine(outputFolder, $"{i}.png");
doc.ActiveWindow.Selection.Export(outputFileName, WdExportFormat.wdExportFormatPNG);
Console.WriteLine($"Page {i} exported to {outputFileName}.");
}
// 关闭Word文档和应用程序
doc.Close();
app.Quit();
}
}
}
原文地址: https://www.cveoy.top/t/topic/b2lQ 著作权归作者所有。请勿转载和采集!