以下是使用C#编写Word转PDF程序的示例代码:

using Microsoft.Office.Interop.Word;
using System.IO;

namespace WordToPdfConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建Word应用程序对象
            Application wordApp = new Application();

            // 获取要转换的Word文档路径
            string wordFilePath = @"C:\Users\user\Desktop\example.docx";

            // 打开Word文档
            Document wordDoc = wordApp.Documents.Open(wordFilePath);

            // 获取PDF保存路径
            string pdfFilePath = Path.ChangeExtension(wordFilePath, ".pdf");

            // 将Word文档另存为PDF格式
            wordDoc.SaveAs2(pdfFilePath, WdSaveFormat.wdFormatPDF);

            // 关闭Word文档和应用程序
            wordDoc.Close();
            wordApp.Quit();
        }
    }
}

需要注意的是,上述代码需要引用Microsoft.Office.Interop.Word命名空间,该命名空间包含了与Microsoft Word应用程序的交互所需的类型和成员。此外,需要安装Microsoft Word应用程序以及其对应的版本的Microsoft Office开发人员工具才能使用Interop组件。

用C#写一个word转pdf的程序

原文地址: https://www.cveoy.top/t/topic/b2h0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录